接口简化
eg:
@interface AllWeatherRadial : Tire { float rainHanding; float snowHanding; } -(void) setRainHanding:(float) rainHanding; -(float) rainHanding; -(void) setSnowHanding:(float)snowHanding; -(float) snowHanding; @end //AllWeatherRadial
简化后的代码:
@interface AllWeatherRadial : Tire { float rainHanding; float snowHanding; } @property float rainHanding; //表明AllWeatherRadial类的对象具有float类型的属性,其名称为rainHanding @property float snowHanding; @end //AllWeatherRadial
@property 是一种新的编译功能,表示声明了一个新对象的属性。
简化实现(简化前的实现省略)
@implementation AllWatherRadial @synthesize rainHanding; @synthesize snowHanding; -(id) initWithPressure:(float) ptreadDepth:(float) td { if(self = [super initWithPressure:ptreadDepth: td]) { rainHanding = 23.7; snowHanding = 32; } return (self); } //initWithPressure -(NSString *) description { ... return (..) } @end
@synthesize 也是一种新的编译器功能,表示“创建该属性的访问器”。当遇到代码@synthesize rainHandling;时,编译器将输出 -setRainHanding:和-rainHandling 方法的已编译的代码。