//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; }
|