编程笔记(Objective-c:属性的实现)

转载请标明出处:blog.csdn.net/zhangxingping
    前面的示例程序中已经看到了使用@synthesize关键字来告诉编译器自动生成属性的getter和setter方法。也就是说如果我们没有显示地在@implementation代码块中实现属性的getter和setter方法,编译器会自动根据property的attributes来生成相应的getter和setter。其实我们在使用@synthesize指令的时候还可以为属性指定特定的实例变量。此时,对属性的操作相当于是对该实例变量的操作。通用形式如下:

@synthesize 属性名称 = 实例变量名称;

例如,之前的程序可以修改为:

Student.h:

[cpp]  view plain copy
  1. #import <Foundation/Foundation.h>  
  2. @interface Student : NSObject  
  3. {  
  4. @public     //将下面的三个实例变量修改为是公有的,以便输出其值,和属性的值进行对比  
  5.     NSString * name;  //学生的姓名  
  6. <pre name="code" class="cpp">      
float math; //数学科目的成绩 float english; //英语科目的成绩 }//@property (retain) NSString * name;@property (retain) NSString * fullName;@property float math;@property float english; -(id)initWithName:(NSString*)aName math:(float)scoreMath english:(float)scoreEnglish;@end
 

Student.m:

[cpp]  view plain copy
  1. #import "Student.h"  
  2. @implementation Student  
  3. //@synthesize name;  
  4. //为fullName属性指定实例变量为name,以后对fullName的操作相当于是对name的操作  
  5. @synthesize fullName = name;  
  6. @synthesize math;  
  7. @synthesize english;  
  8.    
  9. - (id)init  
  10. {  
  11.     self = [super init];  
  12.     if (self)  
  13.     {  
  14.         name = nil;  
  15.         math = 0;  
  16.         english = 0;  
  17.     }  
  18.      
  19.     return self;  
  20. }  
  21.    
  22. -(id)initWithName:(NSString*)aName math:(float)scoreMath english:(float)scoreEnglish  
  23. {  
  24.     self = [super init];  
  25.     if (self)  
  26.     {  
  27.         name = aName;  
  28.         math = scoreMath;  
  29.         english = scoreEnglish;  
  30.     }  
  31.      
  32.     return self;  
  33. }  
  34.    
  35. -(void) dealloc  
  36. {  
  37.     [super dealloc];  
  38. }  
  39. @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 fullName]); //输出属性的值  
  12.     NSLog(@"Name:%@",p->name);       //输出实例变量的值  
  13.     NSLog(@"Math:%f",[p math]);  
  14.     NSLog(@"English:%f",[p english]);  
  15.      
  16.     //[p setName:@"Tony"];  
  17.     [p setFullName:@"Tony"];   //设置属性的值  
  18.     [p setMath:99.0f];  
  19.     [p setEnglish:89.98f];     
  20.    
  21.     NSLog(@"Name:%@",[p fullName]);  //输出属性的值  
  22.     NSLog(@"Name:%@",p->name);       //输出实例变量的值  
  23.     NSLog(@"Math:%f",[p math]);  
  24.     NSLog(@"English:%f",[p english]);  
  25.    
  26.     [p release];  
  27.     [pool drain];  
  28.     return 0;  
  29. }  


上面程序的输出如下:

Name:Mark

Name:Mark

Math:80.000000

English:100.000000

Name:Tony

Name:Tony

Math:99.000000

English:89.980003

        可见这种为属性指定实例变量的方法使得对属性的操作变成了对实例变量的操作。那么在之前的程序中,我们并没有为属性指定实例变量,此时对属性的操作也是相当于对实例变量的操作嘛?如果是,那是对那些实例变量的操作呢?我们来看看下面程序的运行结果:

Student.h文件:

[cpp]  view plain copy
  1. #import <Foundation/Foundation.h>  
  2. @interface Student : NSObject  
  3. {  
  4. @public  
  5.     NSString * name;  //学生的姓名  
  6.     float math;       //数学科目的成绩  
  7.     float english;    //英语科目的成绩   
  8.      
  9. }  
  10. //下面三个属性的类型,名称和类的三个实例变量的类型,名称完全相同  
  11. @property (retain) NSString * name;  
  12. @property float math;  
  13. @property float english;  
  14.    
  15. -(id)initWithName:(NSString*)aName math:(float)scoreMath english:(float)scoreEnglish;  
  16.    
  17. @end  
  18.    
Student.m文件:

[cpp]  view plain copy
  1. #import "Student.h"  
  2. @implementation Student  
  3. //synthesize 指令并没有为这三个属性指定实例变量  
  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.    
  35.    
  36. -(void) dealloc  
  37. {  
  38.     [name release];  
  39.      
  40.     [super dealloc];  
  41. }  
  42.    
  43. @end  
  44.    
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 name]);  
  12.     NSLog(@"Name:%@",p->name);  
  13.     NSLog(@"Math:%f",[p math]);  
  14.     NSLog(@"Math:%f",p->math);  
  15.     NSLog(@"English:%f",[p english]);  
  16.     NSLog(@"English:%f",p->english);  
  17.      
  18.     [p setName:@"Tony"];  
  19.     [p setMath:99.0f];  
  20.     [p setEnglish:89.98f];     
  21.    
  22.     NSLog(@"Name:%@",[p name]);  
  23.     NSLog(@"Name:%@",p->name);  
  24.     NSLog(@"Math:%f",[p math]);  
  25.     NSLog(@"Math:%f",p->math);  
  26.     NSLog(@"English:%f",[p english]);    
  27.     NSLog(@"English:%f",p->english);    
  28.      
  29.     [p release];  
  30.    
  31.     [pool drain];  
  32.     return 0;  
  33. }  

上面程序的输出为:

Name:Mark

Name:Mark

Math:80.000000

Math:80.000000

English:100.000000

English:100.000000

Name:Tony

Name:Tony

Math:99.000000

Math:99.000000

English:89.980003

English:89.980003

        可见在使用@synthesize指令的时候,如果没有为属性指定实例变量,并且属性的类型和名称与实例变量的类型和名称相同的情况下,会自动为该属性指定了同名的实例变量。那如果没有为属性指定实例变量,且类中没有与该属性类型和名称都相同的实例变量,会出现什么样的情况了?例如,将上述程序中对name属性的声明,实现以及调用都修改为Name,完整程序如下:

Student.h文件

[cpp]  view plain copy
  1. #import <Foundation/Foundation.h>  
  2. @interface Student : NSObject  
  3. {  
  4. @public  
  5.     NSString * name;  //学生的姓名  
  6.     float math;       //数学科目的成绩  
  7.     float english;    //英语科目的成绩   
  8.      
  9. }  
  10. @property (retain) NSString * Name; //此处Name首字母为大写,以便和类的实例变量name进行区分  
  11. @property float math;  
  12. @property float english;  
  13.    
  14. -(id)initWithName:(NSString*)aName math:(float)scoreMath english:(float)scoreEnglish;  
  15.    
  16. @end  

Student.m文件:

[cpp]  view plain copy
  1. #import "Student.h"  
  2. @implementation Student  
  3. @synthesize Name; //此处Name首字母为大写,以便和类的实例变量name进行区分  
  4. @synthesize math;  
  5. @synthesize english;  
  6. - (id)init  
  7. {  
  8.     self = [super init];  
  9.     if (self)  
  10.     {  
  11.         name = nil;  
  12.         math = 0;  
  13.         english = 0;  
  14.     }  
  15.      
  16.     return self;  
  17. }  
  18.    
  19. -(id)initWithName:(NSString*)aName math:(float)scoreMath english:(float)scoreEnglish  
  20. {  
  21.     self = [super init];  
  22.     if (self)  
  23.     {  
  24.         name = aName;  
  25.         math = scoreMath;  
  26.         english = scoreEnglish;  
  27.     }  
  28.      
  29.     return self;  
  30. }  
  31.    
  32.    
  33.    
  34. -(void) dealloc  
  35. {  
  36.     [Name release];  
  37.      
  38.     [super dealloc];  
  39. }  
  40.    
  41. @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 Name]);  
  12.     NSLog(@"Name:%@",p->name);  
  13.     NSLog(@"Math:%f",[p math]);  
  14.     NSLog(@"Math:%f",p->math);  
  15.     NSLog(@"English:%f",[p english]);  
  16.     NSLog(@"English:%f",p->english);  
  17.      
  18.     [p setName:@"Tony"];  
  19.     [p setMath:99.0f];  
  20.     [p setEnglish:89.98f];     
  21.    
  22.     NSLog(@"Name:%@",[p Name]);  
  23.     NSLog(@"Name:%@",p->name);  
  24.     NSLog(@"Math:%f",[p math]);  
  25.     NSLog(@"Math:%f",p->math);  
  26.     NSLog(@"English:%f",[p english]);    
  27.     NSLog(@"English:%f",p->english);      
  28.     [p release];  
  29.     [pool drain];  
  30.     return 0;  
  31. }  

上面程序的输出如下:

Name:(null)

Name:Mark

Math:80.000000

Math:80.000000

English:100.000000

English:100.000000

Name:Tony

Name:Mark

Math:99.000000

Math:99.000000

English:89.980003

English:89.980003

        从输出结果可以看出显然属性Name和实例变量name不是同一个对象。注意,上面的代码在objectivc-c2.0 以前的版本上可能会报告错误。这是因为之前的版本中要求属性必须指定对应的实例变量。如果不显示指定对应的实例变量,则会以类中同名的实例变量对应,如果没有同名的实例变量,编译就会报告错误。在0bjective-2.0版本中取消了这个限制,因此上面的程序是可以编译通过并且运行的。只是在没有显示指定属性对应的实例变量的时候,也没有同名实例变量的时候,属性单独存在不和任何的实例变量对应。因此上面程序输出结果中的Name:一项,属性和实例变量的值是不一样的。

        除了@synthesize指令外,还有一个用于实现属性的指令:@dynamic。这个指令告诉编译器我们会显示地实现该属性的getter和setter方法,或者是该属性的getter和setter方式是通过别的方式来完成的,比如动态加载代码以及动态方法解析等方式。如果实现属性时使用的是@dynamic指令,却没有通过上述的方式来实现该属性的getter和setter方法,编译是不会报告错误的,但程序会在运行的时候出错。例如下面的程序:

Student.h:

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

Student.m

[cpp]  view plain copy
  1. #import "Student.h"  
  2. @implementation Student  
  3. @dynamic fullName;  
  4. @synthesize math;  
  5. @synthesize english;  
  6.    
  7. - (id)init  
  8. {  
  9.     self = [super init];  
  10.     if (self)  
  11.     {  
  12.         name = nil;  
  13.         math = 0;  
  14.         english = 0;  
  15.     }  
  16.      
  17.     return self;  
  18. }  
  19.    
  20. -(id)initWithName:(NSString*)aName math:(float)scoreMath english:(float)scoreEnglish  
  21. {  
  22.     self = [super init];  
  23.     if (self)  
  24.     {  
  25.         name = aName;  
  26.         math = scoreMath;  
  27.         english = scoreEnglish;  
  28.     }  
  29.      
  30.     return self;  
  31. }  
  32.    
  33.    
  34.    
  35. -(void) dealloc  
  36. {  
  37.     // [name release];  
  38.      
  39.     [super dealloc];  
  40. }  
  41.    
  42. //-(NSString *)fullName  
  43. //{  
  44. //    return  name;  
  45. //}  
  46. //  
  47. //  
  48. //-(void) setFullName:(NSString *)aName  
  49. //{  
  50. //    [aName retain];  
  51. //    [name release];  
  52. //    name = aName;  
  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.     //程序运行时,会在该行处报告异常:unrecognized selector sent to instance 0Xxxxxx  
  12.     NSLog(@"Name:%@",[p fullName]);  
  13.     NSLog(@"Name:%@",p->name);  
  14.     NSLog(@"Math:%f",[p math]);  
  15.     NSLog(@"Math:%f",p->math);  
  16.     NSLog(@"English:%f",[p english]);  
  17.     NSLog(@"English:%f",p->english);  
  18.      
  19.     [p setFullName:@"Tony"];  
  20.     [p setMath:99.0f];  
  21.     [p setEnglish:89.98f];     
  22.    
  23.     NSLog(@"Name:%@",[p fullName]);  
  24.     NSLog(@"Name:%@",p->name);  
  25.     NSLog(@"Math:%f",[p math]);  
  26.     NSLog(@"Math:%f",p->math);  
  27.     NSLog(@"English:%f",[p english]);    
  28.     NSLog(@"English:%f",p->english);  
  29.      
  30.     [p release];  
  31.    
  32.     [pool drain];  
  33.     return 0;  
  34. }  

        可见在使用@dynamic指令后,由于没有实现fullName的getter方法而导致程序异常。将Student.m中的getter和setter方法的注释//去掉,并将dealloc方法中的注释也去掉,再次编译运行程序结果如下:

Name:Mark

Name:Mark

Math:80.000000

Math:80.000000

English:100.000000

English:100.000000

Name:Tony

Name:Tony

Math:99.000000

Math:99.000000

English:89.980003

English:89.980003

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值