——- android培训、IOS培训、期待与您交流! ———-
属性 代替无谓的set-get方法,能减少代码量。
转换一个类来使用属性。先看main()函数:
int main(int argc, const char *argv[])
{
@autoreleasepool
{
Car *car =[[Car alloc] init];
for (int i=0; i<4; i++)
{
AllWeatherRadial *tire;
tire = [[AllWeatherRadial alloc] init];
[tire setRainHandling: 20+i];
[tire setSnowHandling: 28+i];
NSLog(@"tire %'s handling is %.f %.f", i, [tire rainHandling], [tire snowHandling]);
[car setTire: tire atIndex:i];
[tire release];
}
Engine *engine = [[Slant6 alloc] init];
[car setEngine: engine];
[car print];
[car release];
}
return (0);
}
AllWeatherRadial类的接口代码。
#import <Foundation/Foundation.h>
#import "Tire.h"
@interface AllWeatherRadial: Tire
{
float rainHandling;
float snowHandling;
}
- (void) setRainHandling: (float) rainHanding;
- (float) rainHandling;
- (void) setSnowHandling: (float) snowHandling;
- (float) snowHandling;
@end // AllWeatherRadial
具有属性风格的新形式:
#import <Foundation/Foundation.h>
#import "Tire.h"
@interface AllWeatherRadial: Tire
{
float rainHandling;
float snowhandling;
}
@property float rainHandling;
@property float snowHandlng;
@end // AllWeatherRadial
#import "AllweatherRadial.h"
@implmentation AllWeatherRadial
- (id) initWithPressure: (float) p treadDepth: (float) td
{
if (self = [super initWithPressure: p treadDepth: td])
{
rainHandling = 23.7;
snowHandling = 42.5:
}
return (self);
} // initWithPressure: treadDepth
- (void) setRainHandling: (float) rh
{
rainHandling = rh;
} // setRainHandling
-(float) rainHandling
{
return (rainHandling);
} // rainHandling
- (void) setSnowHandling: (float) sh
{
snowHHandling = sh;
} // setSnowHandling
- (float) snowHandling
{
return (snowHandling);
} // snowHandling
- (NSString *) description
{
NSString *desc;
desc = [[NSString alloc] initWithFormat:@"AllWeatherRadial: %.1f / %.1f /%.1f /%.1f", [self pressure], [self treadDepth], [self rainHandling],[self snowHandling]];
return (desc);
} // description
@end // AllWeatherRadial
简化的实现:
#import "AllweatherRadial.h"
@implmentation AllWeatherRadial
@synthesize rainHandling;
@synthesize snowHandling;
- (id) initWithPressure: (float) p treadDepth: (float) td
{
if (self = [super initWithPressure: p treadDepth: td])
{
rainHandling = 23.7;
snowHandling = 42.5:
}
return (self);
} // initWithPressure: treadDepth
- (NSString *) description
{
NSString *desc;
desc = [[NSString alloc] initWithFormat:@"AllWeatherRadial: %.1f / %.1f /%.1f /%.1f", [self pressure], [self treadDepth], [self rainHandling],[self snowHandling]];
return (desc);
} // description
@end // AllWeatherRadial
@synthesize 表示“创建了该属性的访问代码”当遇到@synthesize rainHandling;
编译器将添加实现-setRainHandling:
和 -ranHandling
方法的预编译代码。
如果没有指定实例变量,编译器会自动创建,所以可以省略以下代码:
{
float rainHdandling;
float snowHandling;
}
点表达式
类似下面两行的代码:
[tire setRainHanding: 20 + i];
[tire setSnowHanding: 20 + i];
可以替换成:
tire.rainHanding = 20 + i;
tire.snowHanding = 28 + i;
两种表示方式的结果相同。
用NSLog()
函数输出:
NSLog(@"tire %d's handing is %.f %.f", i, [tire rainHandling], [tire snowHandling]);
替换为点表达式:
NSLog(@"tire %d's handling is %.f %.f", i, tire.rainHandling, tire.snowHandling);
如果点表达式出现在等号的左边,该变量名称的setter方法被调用。如果点表达式出现在对象变量的右边,则该变量名称的getter方法被调用。