首页 > 其他 > 详细

OC开发系列-@property和@synthesize

时间:2018-04-14 00:08:40      阅读:187      评论:0      收藏:0      [点我收藏+]

property和synthesize关键字

创建一个Person类。

#import <Foundation/Foundation.h>

@interface Person : NSObject
{
    int _age;
    int _height;
}

- (void)setAge:(int)age;
- (int)age;
@end
 
#import "Person.h"
@implementation Person

- (void)setAge:(int)age
{
    _age = age;
}
- (int)age
{
    return _age;
}
@end

开发中考虑封装性将成员属性通过提供setter与getter结构供外界访问。但是这些setter跟getter代码没有任何技术含量。于是苹果提供关键字propertysynthesize关键字利用编译器特性将我们自动生成setter跟getter方法。

@property int age;
// - (void)setAge:(int)age;
// - (int)age;

@synthesize age;
/*
- (void)setAge:(int)age
{

}
- (int)age
{

}
*/

@synthesize age虽然帮我们实现了set跟get方法的实现,并未指定将外界传递的值对哪个成员属性进行赋值。如上Person类需要给成员_age复制。

@synthesize age = _age;

OC开发系列-@property和@synthesize

原文:https://www.cnblogs.com/CoderHong/p/8824866.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!