OC3_MyRect

//

//  MyRect.h

//  OC3_MyRect

//

//  Created by zhangxueming on 15/6/9.

//  Copyright (c) 2015年 zhangxueming. All rights reserved.

//



#import <Foundation/Foundation.h>



@interface MyRect : NSObject

{

    int _length;

    int _width;

}



- (id)initWithLength:(int)length andWidth:(int)width;



- (int)length;



- (int)width;



- (void)setLength:(int)length andWidth:(int)width;



- (int)area;



- (void)printArea;



@end
//

//  MyRect.m

//  OC3_MyRect

//

//  Created by zhangxueming on 15/6/9.

//  Copyright (c) 2015年 zhangxueming. All rights reserved.

//



#import "MyRect.h"



@implementation MyRect



- (id)initWithLength:(int)length andWidth:(int)width

{

    self = [super init] ;

    if (self) {

        _length = length;

        _width = width;

    }

    return self;

}



- (int)length

{

    return _length;

}



- (int)width

{

    return _width;

}



- (void)setLength:(int)length andWidth:(int)width

{

    _length = length;

    _width = width;

}



- (int)area;

{

    return [self length] * [self width];

}



- (void)printArea

{

    NSLog(@"area = %i", [self area]);

}



@end
//

//  main.m

//  OC3_MyRect

//

//  Created by zhangxueming on 15/6/9.

//  Copyright (c) 2015年 zhangxueming. All rights reserved.

//



#import <Foundation/Foundation.h>

#import "MyRect.h"



int main(int argc, const char * argv[]) {

    @autoreleasepool {

        MyRect *rect = [[MyRect alloc] init];

        [rect setLength:10 andWidth:5];

        [rect printArea];

    }

    return 0;

}

 

你可能感兴趣的:(oc)