OC基础学习9:属性

1 使用属性

  • 接口代码简化
    没有使用属性:
#import "Tire.h"

@interface AllWeatherRadial : Tire
{
    // 轮胎在潮湿和积雪的道路上的性能
    float rainHandling;
    float snowHandling;
}
- (void) setRainHandling:(float)rainHandling;
- (float) rainHandling;
- (void) setSnowHandling:(float)snowHandling;
- (float) snowHandling;
@end

使用属性:

@interface AllWeatherRadial : Tire
{
    // 轮胎在潮湿和积雪的道路上的性能
    float rainHandling;
    float snowHandling;
}

@property float rainHandling;
@property float snowHandling;

@end

@ property预编译指令的作用是自动声明属性的setter和getter方法。

  • 简化实现代码
    之前:
#import "AllWeatherRadial.h"

@implementation AllWeatherRadial
-(NSString *)description
{
//    return (@"I am a tire for rain or shine.");
    NSString *desc;
    desc = [[NSString alloc] initWithFormat:@"AllWeatherRadial: %.1f / %.1f / %.1f / %.1f.", self.pressure, self.treadDepth, self.rainHandling, self.snowHandling];
    return desc;
}
- (void)setRainHandling:(float)r
{
    rainHandling = r;
}
- (float)rainHandling
{
    return rainHandling;
}
- (void)setSnowHandling:(float)s
{
    snowHandling = s;
}
- (float)snowHandling
{
    return snowHandling;
}

- (id) initWithPressure:(float)p treadDepth:(float)td
{
    if (self = [super initWithPressure:p treadDepth:td]) {
        rainHandling = 23.7;
        snowHandling = 58.1;
    }
    return self;
}
@end

使用属性后的实现代码:

#import "AllWeatherRadial.h"

@implementation AllWeatherRadial

@synthesize rainHandling;
@synthesize snowHandling;

-(NSString *)description
{
    NSString *desc;
    desc = [[NSString alloc] initWithFormat:@"AllWeatherRadial: %.1f / %.1f / %.1f / %.1f.", self.pressure, self.treadDepth, self.rainHandling, self.snowHandling];
    return desc;
}

- (id) initWithPressure:(float)p treadDepth:(float)td
{
    if (self = [super initWithPressure:p treadDepth:td]) {
        rainHandling = 23.7;
        snowHandling = 58.1;
    }
    return self;
}
@end

@synthesize预编译指令表示通知编译器生成访问方法。当遇到@synthesize xxx;这行代码时,编译器将添加实现-setXxx:和-xxx方法的预编译代码。

该@synthesize预编译指令不同于代码生成。你永远看不到实现-setXxx:和-xxx的代码,但是这些方法确实存在并可以被调用。这种技术使苹果公司可以更加灵活地改变Objective-C中生成访问方法的方式,并获得更安全的实现和更高的的性能。在Xcode4.5以后的版本中,可以不必使用@synthesize了。

  • 所有的属性都是基于变量的,在@synthesize时,编译器会自动创建与属性名称相同的实例变量。

    实例变量声明位置:

    • 头文件:有子类,想从子类直接通过属性访问变量。
    • 实现文件:变量只属于当前类。
  • 点表达式
    之前的
    [tire setPressure:23+i];
    可相应变为:
    tire.rainHandling = 23+i;

2 属性扩展

  • 实例变量和属性不是同一个名称
@interface Car : NSObject
{
    NSMutableArray *tires;
    Engine *engine;
    NSString *appellation;
}
@property (copy) NSString *name;
@property (retain) Engine *engine;

修改@synthesize指令为: @synthesize name = appellation;
编译器仍将创建-setName:-name方法,但在其实现代码中用的确实appellation实例变量。

  • 只读属性
    readwrite readonly
    属性为只读时,编译器只会生成一个getter方法。

  • 属性不支持那些需要接收额外参数的方法。如car对象中tire对象的代码。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 重点掌握 3 类对象和方法 对象就是一个物体 类的独特存在就是一个实例,对实例进行操作叫做方法。方法可以应用于类或...
    Coder大雄阅读 5,030评论 0 2
  • 苹果官方文档翻译 《Objective-C语言编程》(Programming with Objective-C) ...
    fever105阅读 26,166评论 19 129
  • 昨天,接到妈妈的电话。电话那头的她高兴的像个孩子:“我告诉你一个好消息,你爸爸能起床了!我今天把他扶起来走动、...
    无忧晨阅读 1,589评论 0 1
  • “叮铃铃,叮铃铃”,上课啦,汤老师,给我们一人,发了一张长方形白纸。同学们满怀疑问的问汤老师,“给我们发一张白纸要...
    嘉颖_952e阅读 2,350评论 0 1
  • 白墙青瓦草愈低,风细细,欲把相思化诗意,下不成笔。庙堂人家江湖远,雨沥沥,三寸凡心减两寸,豁然生喜……!
    浮萍一笑阅读 1,283评论 0 2

友情链接更多精彩内容