编程笔记(objective-c :关于属性)

关于属性

        在C++中,通常需要编写Getter和Setter方法来获取或者是设置实例变量的值。这两种方法需要在程序中显示的完成。这种方式在Objective-C中也是适用的。但是Objective-C提供了一种更为便捷的方式来完成这种功能。它就是属性。和C++中的显示的Getter和Setter方法相比,属性机制使得Getter函数和Setter来的更容易和更简化。

Objectivc-C中的属性是通过关键字@property来声明的。

例如,有类如下:

Student.h文件:

[cpp]  view plain copy
  1. #import <Foundation/Foundation.h>  
  2.    
  3.    
  4. @interface Student : NSObject  
  5. {  
  6. @private  
  7.     NSString * name;  //学生的姓名  
  8.     float math;       //数学科目的成绩  
  9.     float english;    //英语科目的成绩   
  10.      
  11. }  
  12.    
  13.    
  14. -(id)initWithName:(NSString*)aName math:(float)scoreMath english:(float)scoreEnglish;  
  15.    
  16. -(NSString *)getName;  
  17. -(float)getMath;  
  18. -(float)getEnglish;  
  19.    
  20. @end  

Student.m文件:

[cpp]  view plain copy
  1. #import "Student.h"  
  2.    
  3. @implementation Student  
  4.    
  5. - (id)init  
  6. {  
  7.     self = [super init];  
  8.     if (self)  
  9.     {  
  10.         name = nil;  
  11.         math = 0;  
  12.         english = 0;  
  13.     }  
  14.      
  15.     return self;  
  16. }  
  17.    
  18. -(id)initWithName:(NSString*)aName math:(float)scoreMath english:(float)scoreEnglish  
  19. {  
  20.     self = [super init];  
  21.     if (self)  
  22.     {  
  23.         name = aName;  
  24.         math = scoreMath;  
  25.         english = scoreEnglish;  
  26.     }  
  27.      
  28.     return self;  
  29. }  
  30.    
  31. -(NSString *)getName  
  32. {  
  33.     return name;  
  34. }  
  35.    
  36. -(float)getMath  
  37. {  
  38.     return math;  
  39. }  
  40.    
  41. -(float)getEnglish  
  42. {  
  43.     return english;  
  44. }  
  45.    
  46. - (void)dealloc  
  47. {  
  48.     [super dealloc];  
  49. }  
  50.    
  51. @end  

main.m文件如下:

 #import <Foundation/Foundation.h>

[cpp]  view plain copy
  1. #import "Student.h"  
  2.    
  3. int main (int argc, const char * argv[])  
  4. {  
  5.    
  6.     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];  
  7.      
  8.     Student * p = [[Student alloc] initWithName:@"Mark" math:80.0f english:100.00f];  
  9.      
  10.     NSLog(@"Name:%@",[p getName]);  
  11.     NSLog(@"Math:%f",[p getMath]);  
  12.     NSLog(@"Math:%f",[p getEnglish]);     
  13.      
  14.     [p release];  
  15.    
  16.     [pool drain];  
  17.     return 0;  
  18. }  

上面程序的输出结果如下:

Name:Mark

Math:80.000000

Math:100.000000

上面的程序是通过C++中的方式来编写Getter方法来获取私有的实例变量的值。这些个Getter方法实现虽然简单,但是必需有程序员显示书写来完成。应用属性机制后,程序如下:

Student.h文件:

[cpp]  view plain copy
  1. #import <Foundation/Foundation.h>  
  2.    
  3. @interface Student : NSObject  
  4. {  
  5. @private  
  6.     NSString * name;  //学生的姓名  
  7.     float math;       //数学科目的成绩  
  8.     float english;    //英语科目的成绩   
  9.      
  10. }  
  11.    
  12. @property NSString * name;  
  13. @property float math;  
  14. @property float english;  
  15.    
  16.    
  17.    
  18. -(id)initWithName:(NSString*)aName math:(float)scoreMath english:(float)scoreEnglish;  
  19.    
  20. //-(NSString *)getName;  
  21. //-(float)getMath;  
  22. //-(float)getEnglish;  
  23.    
  24. @end  

Student.m文件:

[cpp]  view plain copy
  1. #import "Student.h"  
  2. @implementation Student  
  3.    
  4. @synthesize name;  
  5. @synthesize math;  
  6. @synthesize english;  
  7.    
  8. - (id)init  
  9. {  
  10.     self = [super init];  
  11.     if (self)  
  12.     {  
  13.         name = nil;  
  14.         math = 0;  
  15.         english = 0;  
  16.     }  
  17.      
  18.     return self;  
  19. }  
  20.    
  21. -(id)initWithName:(NSString*)aName math:(float)scoreMath english:(float)scoreEnglish  
  22. {  
  23.     self = [super init];  
  24.     if (self)  
  25.     {  
  26.         name = aName;  
  27.         math = scoreMath;  
  28.         english = scoreEnglish;  
  29.     }  
  30.      
  31.     return self;  
  32. }  
  33.    
  34. //-(NSString *)getName  
  35. //{  
  36. //    return name;  
  37. //}  
  38. //  
  39. //-(float)getMath  
  40. //{  
  41. //    return math;  
  42. //}  
  43. //  
  44. //-(float)getEnglish  
  45. //{  
  46. //    return english;  
  47. //}  
  48. //  
  49. //- (void)dealloc  
  50. //{  
  51. //    [super dealloc];  
  52. //}  
  53.    
  54. @end  

main.m文件如下: 

[cpp]  view plain copy
  1. #import <Foundation/Foundation.h>  
  2. #import "Student.h"  
  3.    
  4. int main (int argc, const char * argv[])  
  5. {  
  6.    
  7.     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];  
  8.      
  9.     Student * p = [[Student alloc] initWithName:@"Mark" math:80.0f english:100.00f];  
  10.      
  11. //  NSLog(@"Name:%@",[p getName]);  
  12. //  NSLog(@"Math:%f",[p getMath]);  
  13. //  NSLog(@"Math:%f",[p getEnglish]);     
  14.     NSLog(@"Name:%@",p.name);  
  15.     NSLog(@"Math:%f",p.math);  
  16.     NSLog(@"Math:%f",p.english);    
  17.      
  18.     [p release];  
  19.    
  20.     [pool drain];  
  21.     return 0;  
  22. }  

程序的输出如下:

Name:Mark

Math:80.000000

Math:100.000000

可见属性机制使得程序更加简洁明了。

上面程序中引入了新的关键字@synthesize,这个关键字告诉编译器自动为其后面的属性生成Getter()和Setter()方法。需要注意的一点是虽然在描述上适用的是“自动生成Getter()和Setter()方法”,但是实际上我们并看不到生成的对应代码。需要明确的是编译器自动生成的Getter方法:name(),math(),english()以及Sette方法:setName(), setMath(), setEnglish()的调用完全和普通的方法是一样的。例如我们可以修改上面的main()函数如下:

 

[cpp]  view plain copy
  1. #import <Foundation/Foundation.h>  
  2. #import "Student.h"  
  3.    
  4. int main (int argc, const char * argv[])  
  5. {  
  6.    
  7.     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];  
  8.      
  9.     Student * p = [[Student alloc] initWithName:@"Mark" math:80.0f english:100.00f];  
  10.      
  11. //  NSLog(@"Name:%@",[p getName]);  
  12. //  NSLog(@"Math:%f",[p getMath]);  
  13. //  NSLog(@"Math:%f",[p getEnglish]);  
  14.      
  15.      
  16. //    NSLog(@"Name:%@",p.name);  
  17. //    NSLog(@"Math:%f",p.math);  
  18. //    NSLog(@"Math:%f",p.english);    
  19.      
  20.     [p setName:@"Tony"];  
  21.     [p setMath:99.0f];  
  22.     [p setEnglish:89.98f];     
  23.    
  24.     NSLog(@"Name:%@",[p name]);  
  25.     NSLog(@"Math:%f",[p math]);  
  26.     NSLog(@"Math:%f",[p english]);    
  27.    
  28.    
  29.      
  30.     [p release];  
  31.    
  32.     [pool drain];  
  33.     return 0;  
  34. }  

程序输出为:

Name:Tony

Math:99.000000

Math:89.980003

        上面的程序在Xcode中编译后,会在@property NSString * name;代码的所在行头看到一个黄色的叹号,这表明编译器在该行代码处给出了编译警告。单击该黄色叹号可以看到给出的警告信息:“No 'assign', 'retain', or 'copy' attribute is specified - 'assign' is assumed”和“Default property attribute 'assign' not appropriate for non-gc object”。警告信息的意思是:“没有明确指出应该是assign还是retain或者是copy,却省的是assign”和“缺省得属性设置assign不适合非gc对象 ”,那么这两个警告信息的含义具体是什么呢?assign,retian,copy又分别代表什么含义呢?什么是gc对象呢?什么是非gc对象呢?这些问题见后文描述。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值