@synthesize 属性名称 = 实例变量名称;
例如,之前的程序可以修改为:
Student.h:
- #import<Foundation/Foundation.h>
- @interfaceStudent:NSObject
- {
- @public//将下面的三个实例变量修改为是公有的,以便输出其值,和属性的值进行对比
- NSString*name;//学生的姓名
- <prename="code"class="cpp">
Student.m:
- #import"Student.h"
- @implementationStudent
- //@synthesizename;
- //为fullName属性指定实例变量为name,以后对fullName的操作相当于是对name的操作
- @synthesizefullName=name;
- @synthesizemath;
- @synthesizeenglish;
- -(id)init
- {
- self=[superinit];
- if(self)
- {
- name=nil;
- math=0;
- english=0;
- }
- returnself;
- }
- -(id)initWithName:(NSString*)aNamemath:(float)scoreMathenglish:(float)scoreEnglish
- {
- self=[superinit];
- if(self)
- {
- name=aName;
- math=scoreMath;
- english=scoreEnglish;
- }
- returnself;
- }
- -(void)dealloc
- {
- [superdealloc];
- }
- @end
main.m:
- #import<Foundation/Foundation.h>
- #import"Student.h"
- intmain(intargc,constchar*argv[])
- {
- NSAutoreleasePool*pool=[[NSAutoreleasePoolalloc]init];
- Student*p=[[Studentalloc]initWithName:@"Mark"math:80.0fenglish:100.00f];
- NSLog(@"Name:%@",[pfullName]);//输出属性的值
- NSLog(@"Name:%@",p->name);//输出实例变量的值
- NSLog(@"Math:%f",[pmath]);
- NSLog(@"English:%f",[penglish]);
- //[psetName:@"Tony"];
- [psetFullName:@"Tony"];//设置属性的值
- [psetMath:99.0f];
- [psetEnglish:89.98f];
- NSLog(@"Name:%@",[pfullName]);//输出属性的值
- NSLog(@"Name:%@",p->name);//输出实例变量的值
- NSLog(@"Math:%f",[pmath]);
- NSLog(@"English:%f",[penglish]);
- [prelease];
- [pooldrain];
- return0;
- }
上面程序的输出如下:
Name:Mark
Name:Mark
Math:80.000000
English:100.000000
Name:Tony
Name:Tony
Math:99.000000
English:89.980003
可见这种为属性指定实例变量的方法使得对属性的操作变成了对实例变量的操作。那么在之前的程序中,我们并没有为属性指定实例变量,此时对属性的操作也是相当于对实例变量的操作嘛?如果是,那是对那些实例变量的操作呢?我们来看看下面程序的运行结果:
Student.h文件:
- #import<Foundation/Foundation.h>
- @interfaceStudent:NSObject
- {
- @public
- NSString*name;//学生的姓名
- floatmath;//数学科目的成绩
- floatenglish;//英语科目的成绩
- }
- //下面三个属性的类型,名称和类的三个实例变量的类型,名称完全相同
- @property(retain)NSString*name;
- @propertyfloatmath;
- @propertyfloatenglish;
- -(id)initWithName:(NSString*)aNamemath:(float)scoreMathenglish:(float)scoreEnglish;
- @end
- #import"Student.h"
- @implementationStudent
- //synthesize指令并没有为这三个属性指定实例变量
- @synthesizename;
- @synthesizemath;
- @synthesizeenglish;
- -(id)init
- {
- self=[superinit];
- if(self)
- {
- name=nil;
- math=0;
- english=0;
- }
- returnself;
- }
- -(id)initWithName:(NSString*)aNamemath:(float)scoreMathenglish:(float)scoreEnglish
- {
- self=[superinit];
- if(self)
- {
- name=aName;
- math=scoreMath;
- english=scoreEnglish;
- }
- returnself;
- }
- -(void)dealloc
- {
- [namerelease];
- [superdealloc];
- }
- @end
- #import<Foundation/Foundation.h>
- #import"Student.h"
- intmain(intargc,constchar*argv[])
- {
- NSAutoreleasePool*pool=[[NSAutoreleasePoolalloc]init];
- Student*p=[[Studentalloc]initWithName:@"Mark"math:80.0fenglish:100.00f];
- NSLog(@"Name:%@",[pname]);
- NSLog(@"Name:%@",p->name);
- NSLog(@"Math:%f",[pmath]);
- NSLog(@"Math:%f",p->math);
- NSLog(@"English:%f",[penglish]);
- NSLog(@"English:%f",p->english);
- [psetName:@"Tony"];
- [psetMath:99.0f];
- [psetEnglish:89.98f];
- NSLog(@"Name:%@",[pname]);
- NSLog(@"Name:%@",p->name);
- NSLog(@"Math:%f",[pmath]);
- NSLog(@"Math:%f",p->math);
- NSLog(@"English:%f",[penglish]);
- NSLog(@"English:%f",p->english);
- [prelease];
- [pooldrain];
- return0;
- }
上面程序的输出为:
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文件
- #import<Foundation/Foundation.h>
- @interfaceStudent:NSObject
- {
- @public
- NSString*name;//学生的姓名
- floatmath;//数学科目的成绩
- floatenglish;//英语科目的成绩
- }
- @property(retain)NSString*Name;//此处Name首字母为大写,以便和类的实例变量name进行区分
- @propertyfloatmath;
- @propertyfloatenglish;
- -(id)initWithName:(NSString*)aNamemath:(float)scoreMathenglish:(float)scoreEnglish;
- @end
Student.m文件:
- #import"Student.h"
- @implementationStudent
- @synthesizeName;//此处Name首字母为大写,以便和类的实例变量name进行区分
- @synthesizemath;
- @synthesizeenglish;
- -(id)init
- {
- self=[superinit];
- if(self)
- {
- name=nil;
- math=0;
- english=0;
- }
- returnself;
- }
- -(id)initWithName:(NSString*)aNamemath:(float)scoreMathenglish:(float)scoreEnglish
- {
- self=[superinit];
- if(self)
- {
- name=aName;
- math=scoreMath;
- english=scoreEnglish;
- }
- returnself;
- }
- -(void)dealloc
- {
- [Namerelease];
- [superdealloc];
- }
- @end
main.m文件:
- #import<Foundation/Foundation.h>
- #import"Student.h"
- intmain(intargc,constchar*argv[])
- {
- NSAutoreleasePool*pool=[[NSAutoreleasePoolalloc]init];
- Student*p=[[Studentalloc]initWithName:@"Mark"math:80.0fenglish:100.00f];
- NSLog(@"Name:%@",[pName]);
- NSLog(@"Name:%@",p->name);
- NSLog(@"Math:%f",[pmath]);
- NSLog(@"Math:%f",p->math);
- NSLog(@"English:%f",[penglish]);
- NSLog(@"English:%f",p->english);
- [psetName:@"Tony"];
- [psetMath:99.0f];
- [psetEnglish:89.98f];
- NSLog(@"Name:%@",[pName]);
- NSLog(@"Name:%@",p->name);
- NSLog(@"Math:%f",[pmath]);
- NSLog(@"Math:%f",p->math);
- NSLog(@"English:%f",[penglish]);
- NSLog(@"English:%f",p->english);
- [prelease];
- [pooldrain];
- return0;
- }
上面程序的输出如下:
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:
- #import<Foundation/Foundation.h>
- @interfaceStudent:NSObject
- {
- @public
- NSString*name;//学生的姓名
- floatmath;//数学科目的成绩
- floatenglish;//英语科目的成绩
- }
- @property(retain)NSString*fullName;
- @propertyfloatmath;
- @propertyfloatenglish;
- -(id)initWithName:(NSString*)aNamemath:(float)scoreMathenglish:(float)scoreEnglish;
- @end
Student.m
- #import"Student.h"
- @implementationStudent
- @dynamicfullName;
- @synthesizemath;
- @synthesizeenglish;
- -(id)init
- {
- self=[superinit];
- if(self)
- {
- name=nil;
- math=0;
- english=0;
- }
- returnself;
- }
- -(id)initWithName:(NSString*)aNamemath:(float)scoreMathenglish:(float)scoreEnglish
- {
- self=[superinit];
- if(self)
- {
- name=aName;
- math=scoreMath;
- english=scoreEnglish;
- }
- returnself;
- }
- -(void)dealloc
- {
- //[namerelease];
- [superdealloc];
- }
- //-(NSString*)fullName
- //{
- //returnname;
- //}
- //
- //
- //-(void)setFullName:(NSString*)aName
- //{
- //[aNameretain];
- //[namerelease];
- //name=aName;
- //}
- @end
main.m:
- #import<Foundation/Foundation.h>
- #import"Student.h"
- intmain(intargc,constchar*argv[])
- {
- NSAutoreleasePool*pool=[[NSAutoreleasePoolalloc]init];
- Student*p=[[Studentalloc]initWithName:@"Mark"math:80.0fenglish:100.00f];
- //程序运行时,会在该行处报告异常:unrecognizedselectorsenttoinstance0Xxxxxx
- NSLog(@"Name:%@",[pfullName]);
- NSLog(@"Name:%@",p->name);
- NSLog(@"Math:%f",[pmath]);
- NSLog(@"Math:%f",p->math);
- NSLog(@"English:%f",[penglish]);
- NSLog(@"English:%f",p->english);
- [psetFullName:@"Tony"];
- [psetMath:99.0f];
- [psetEnglish:89.98f];
- NSLog(@"Name:%@",[pfullName]);
- NSLog(@"Name:%@",p->name);
- NSLog(@"Math:%f",[pmath]);
- NSLog(@"Math:%f",p->math);
- NSLog(@"English:%f",[penglish]);
- NSLog(@"English:%f",p->english);
- [prelease];
- [pooldrain];
- return0;
- }
可见在使用@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