打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
ios的@property属性和@synthesize属性

    当你定义了一系列的变量时,需要写很多的getter和setter方法,而且它们的形式都是差不多的,,所以Xcode提供了@property和@synthesize属性,@property用在 .h 头文件中用作声明,@synthesize用在.m 文件中用于实现。

如下,新建一个基于“Command Line Tool”的项目,名为“property”,再新建一个Student类,

传统的写法是:

Student.h

  1. //  
  2. //  Student.h  
  3. //  property  
  4. //  
  5. //  Created by Rio.King on 13-8-25.  
  6. //  Copyright (c) 2013年 Rio.King. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10.   
  11. @interface Student : NSObject  
  12. {  
  13.     int age;  
  14.     int no;  
  15. }  
  16.   
  17. //age的getter和setter方法声明  
  18. - (int)age;  
  19. - (void)setAge:(int)newAge;  
  20.   
  21. //no的getter和setter方法声明  
  22. - (int)no;  
  23. - (void)setNo:(int)newNo;  
  24.   
  25. @end  

Student.m
  1. //  
  2. //  Student.m  
  3. //  property  
  4. //  
  5. //  Created by Rio.King on 13-8-25.  
  6. //  Copyright (c) 2013年 Rio.King. All rights reserved.  
  7. //  
  8.   
  9. #import "Student.h"  
  10.   
  11. @implementation Student  
  12.   
  13. //age的getter和setter方法的实现  
  14. - (int)age  
  15. {  
  16.     return age;  
  17. }  
  18. -(void)setAge:(int)newAge  
  19. {  
  20.     age = newAge;  
  21. }  
  22.   
  23. //no的getter和setter方法的实现  
  24. - (int)no  
  25. {  
  26.     return no;  
  27. }  
  28. - (void)setNo:(int)newNo  
  29. {  
  30.     no = newNo;  
  31. }  
  32.   
  33. @end  

main.m
  1. //  
  2. //  main.m  
  3. //  property  
  4. //  
  5. //  Created by Rio.King on 13-8-25.  
  6. //  Copyright (c) 2013年 Rio.King. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10. #import "Student.h"   
  11.   
  12. int main(int argc, const char * argv[])  
  13. {  
  14.   
  15.     @autoreleasepool {  
  16.           
  17.         // insert code here...  
  18.         Student *stu = [[Student alloc] init];  
  19.         stu.age = 100;//这句相当于setter方法  
  20.         NSLog(@"age is %i", stu.age);//这里的 stu.age 相当于getter方法  
  21.           
  22.         [stu release];  
  23.           
  24.     }  
  25.     return 0;  
  26. }  

------------------------------------------------------------------------------------------------------------------------用@property和@synthesize的写法是:

 Student.h

  1. //  
  2. //  Student.h  
  3. //  property  
  4. //  
  5. //  Created by Rio.King on 13-8-25.  
  6. //  Copyright (c) 2013年 Rio.King. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10.   
  11. @interface Student : NSObject  
  12. {  
  13.     int age;  
  14.     int no;  
  15. }  
  16.   
  17. //当编译器遇到@property时,会自动展开成getter和setter的声明  
  18. @property int age;  
  19. @property int no;  
  20.   
  21.   
  22. @end  

Student.m
  1. //  
  2. //  Student.m  
  3. //  property  
  4. //  
  5. //  Created by Rio.King on 13-8-25.  
  6. //  Copyright (c) 2013年 Rio.King. All rights reserved.  
  7. //  
  8.   
  9. #import "Student.h"  
  10.   
  11. @implementation Student  
  12.   
  13. //@synthesize 会自动生成getter和setter的实现  
  14. //@synthesize 默认会去访问age,no,height同名的变量,,  
  15. //如果找不到同名的变量,会在内部自动生成一个私有同名变量age,no,height,,  
  16. //因此Student.h 中的这几个变量也可以省略不写。  
  17. @synthesize age,no;  
  18.   
  19. @end  

main.m
  1. //  
  2. //  main.m  
  3. //  property  
  4. //  
  5. //  Created by Rio.King on 13-8-25.  
  6. //  Copyright (c) 2013年 Rio.King. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10. #import "Student.h"  
  11.   
  12. int main(int argc, const char * argv[])  
  13. {  
  14.       
  15.     @autoreleasepool {  
  16.           
  17.         // insert code here...  
  18.         Student *stu = [[Student alloc] init];  
  19.         stu.age = 100;  
  20.         NSLog(@"age is %i", stu.age);  
  21.           
  22.         [stu release];  
  23.     }  
  24.     return 0;  
  25. }  

几点说明:

1.在Xcode4.5及以后的版本中,可以省略@synthesize ,编译器会自动帮你加上getter 和 setter 方法的实现,并且默认会去访问

_age这个成员变量,如果找不到_age这个成员变量,会自动生成一个叫做 _age私有成员变量。

2.视频教学中建议变量名用"_"前缀作为开头,但我看big Nerd 那本书里是不用的,个人也比较习惯 big Nerd 的那种写法,所以变量名就不加前缀了。Y^o^Y 




本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
@property@synthesize自定义构造方法
@property跟成员变量区别
@synthesize
代码戒律:Objective
52个有效方法(6) - 理解“属性”这一概念
OC属性与实例变量
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服