【Objective-C】10-@property和@synthesize



一、@property和@synthesize的作用:

      由于在oc语法中对成员变量的完整访问都需要先在@interface和@end之间声明成员变量,然后再声明这个成员变量的get方法和set方法。并且在@implementation和@end之间实现这个成员变量的get方法和set方法。这种方式在成员变量比较多的情况下比较烦琐,因此xcode中引入了@property和@synthesize关键字来简化成员变量及它的get方法和set方法的声明和实现。


二、 在Xcode4.4版本及之后版本中的@property

    2.1 在新版本中,只需要使用@property int age即可完成以下功能:

    1>如果成员变量不存在,默认生成私有变量_age。如果要生成子类可以访问的成员变量,需要在类的声明中手动声明@protect int _age;

   2>可以自动实现age属性的get和setter的声明
   3>可以自动实现age属性的get和setter的实现
   
   2.2 注意
   1>使用@property时,变量名尽量不要使用下划线开头。如@property int age; 不要声明成@property int _age;
   2>如果成员变量不存在,@property默认会自动生成@private并以下划线开头的成员变量
   3>如果get方法存在,则@property只会自动生成set方法。
        如果set方法存在,则@property只会自动生成get方法。
        并且如果成员变量不存在,会自动生成@private的成员变量。
        如果set方法和get方法都存在,则@property不会自动生成get方法和set方法,并且不会自动生成成员变量。

三、在Xcode4.4版本之前
   3.1 @property
      1>作用:
          @property关键字可以自动某个成员变量的set方法和get方法的声明
      2>语法:@property int age;
           当编译器执行到这一句时,会自动解析成:
       - (void)setAge:(int)age;
       - (int)age;

       3>注意
           @property声明的变量不要以下划线开头。
           @property要声明在@interface和@end之间。
     
     3.2 @synthesize
        1>作用:
             @synthesize关键字可以自动生成某个成员变量的set方法和get方法的实现
        2>语法:@synthesize age=_age;
             当编译器执行到这一句时,会自动解析成:
             - (void)setAge:(int)age
             {
                  _age=age;
             }
             - (int)age
             {
                   return _age;
             }
3>注意:
   (1)@synthesize age=_age; 等号后面的_age表示访问的是_age成员变量。如果只是单纯的声明为@synthesize age;则默认会访问同名的age成员变量。如果成员变量不存在,就会自动生成@private类型的age变量。
   (2)使用@synthesize时,必须在@interface和@end之间有显式声明相应的@property。如果没有的话,会报"Property implementation must have its declaration in interface '类名'”。它的意思是属性必须在声明类的时候显示定义。
   (3)由于@synthesize自动生成的成员变量是@private的,如果子类需要访问该成员变量的话,需手动声明@protect的成员变量。
 (4)@synthesize只能用在@implementation和@end之间。

#import <Foundation/Foundation.h>

@interface Person : NSObject
{
	//int _age; // 年龄
	double _height; // 身高
}
/*
	1.在Xcode4.4之前,@property int age;相当于自动声明age的get方法和set方法。即编译器执行到这一句时,会自动解析成:
	  - (void)setAge:(int)age;
	  - (int)age;
	2.在Xcode4.4及之后版本,@property int age;完成以下功能:
	  1>如果成员变量不存在,默认生成私有变量_age。如果要生成子类可以访问的成员变量,需要在类的声明中手动声明@protect int _age;
	  2>可以自动实现age属性的get和setter的声明。
	  3>可以自动实现age属性的get和setter的实现。
*/
@property int age;//属性名age尽量不要以_开头


/*
	如果get方法存在,则@property只会自动生成set方法。
	如果set方法存在,则@property只会自动生成get方法。
	并且如果成员变量不存在,会自动生成@private的成员变量。
	如果set方法和get方法都存在,则@property不会自动生成get方法和set方法,并且不会自动生成成员变量。
*/
@property double height;


@end

@implementation Person
/*
	1.@synthesize关键字可以自动生成某个成员变量的set方法和get方法的实现.
	当编译器执行到这一句时,会自动解析成:
	- (void)setAge:(int)age
	{
	    _age=age;
	}
	- (int)age
	{
	    return _age;
	}
	2.使用@synthesize时,必须在@interface和@end之间有显式声明相应的@property。如果没有的话,会报"Property imple	mentation must have its declaration in interface '类名'”。它的意思是属性必须在声明类的时候显示定义。
	3.由于@synthesize自动生成的成员变量是@private的,如果子类需要访问该成员变量的话,需手动声明@protect的成员变量。
	4.@synthesize只能用在@implementation和@end之间。
*/
	@synthesize age = _age; //_age表示访问的是_age成员变量。如果成员变量不存在,就会自动生成@private类型的age变量。

	//@synthesize age; // 默认会访问同名的age成员变量。如果成员变量不存在,就会自动生成@private类型的age变量。

	//这里只实现height的get方法,没有实现set方法。但是@property int height会自动实现height的set方法。
	-(int)height
	{
		return _height;
	}
@end


int main() {
	//声明Person类
	Person *p = [Person new];
	//调用age的set方法
	[p setAge : 20];
	[p setHeight : 1.7];
	//调用age和height的get方法并输出信息
	NSLog(@"这个人今年%d岁,%.2f米高了", [p age], [p height]);
	return 0;
}





输出结果:
@synthesize[903:35426] 这个人今年20岁,1.70米高了







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值