黑马程序员技术交流社区

标题: set方法和get方法的声明和实现 [打印本页]

作者: zhuanshu1458    时间: 2015-11-8 22:58
标题: set方法和get方法的声明和实现
//set方法/get方法的声明和实现;


@import<Foundation/Foundation.h>

@interface Car: NSObject
{
    int _wheels;
}
//set方法get方法的声明
- (void)setWheels(int) wheels;
- (int)wheels;

@end

@implementation
//set方法get方法的实现
- (void)setWheels(int) wheels
{
    if(wheels<=0){      //封装的好处:过滤不合理的值
        wheels=1;
    }
    _wheels=wheels;
}
- (int)wheels
{
    return _wheels;
}

@end

int main(){
    Car *car1 = [Car new];
    int aNum=7;
    [car1 aNum];
    [car1 wheels];              //读取wheels的值而不是_wheels
   
   
    return 0;
}


作者: 吴永涛    时间: 2015-11-9 00:35
这么长!
作者: 韦德460    时间: 2015-11-9 09:29
good!!!
作者: 李坤小太阳    时间: 2015-11-9 15:05
good!!!
作者: arhui    时间: 2015-11-9 15:48
写的不错喔




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2