当你定义了一系列的变量时,需要写很多的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. #import <Foundation/Foundation.h>

  9. @interface Student : NSObject
  10. {
  11. int age;
  12. int no;
  13. }

  14. //age的getter和setter方法声明
  15. - (int)age;
  16. - (void)setAge:(int)newAge;

  17. //no的getter和setter方法声明
  18. - (int)no;
  19. - (void)setNo:(int)newNo;

  20. @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. #import "Student.h"

  9. @implementation Student

  10. //age的getter和setter方法的实现
  11. - (int)age
  12. {
  13. return age;
  14. }
  15. -(void)setAge:(int)newAge
  16. {
  17. age = newAge;
  18. }

  19. //no的getter和setter方法的实现
  20. - (int)no
  21. {
  22. return no;
  23. }
  24. - (void)setNo:(int)newNo
  25. {
  26. no = newNo;
  27. }

  28. @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. #import <Foundation/Foundation.h>
  9. #import "Student.h"

  10. int main(int argc, const char * argv[])
  11. {

  12. @autoreleasepool {

  13. // insert code here...
  14. Student *stu = [[Student alloc] init];
  15. stu.age = 100;//这句相当于setter方法
  16. NSLog(@"age is %i", stu.age);//这里的 stu.age 相当于getter方法

  17. [stu release];

  18. }
  19. return 0;
  20. }



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

Student.h

 

  1. // Student.h
  2. // property
  3. //
  4. // Created by Rio.King on 13-8-25.
  5. // Copyright (c) 2013年 Rio.King. All rights reserved.
  6. //

  7. #import <Foundation/Foundation.h>

  8. @interface Student : NSObject
  9. {
  10. int age;
  11. int no;
  12. }

  13. //当编译器遇到@property时,会自动展开成getter和setter的声明
  14. @property int age;
  15. @property int no;


  16. @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. #import "Student.h"

  9. @implementation Student

  10. //@synthesize 会自动生成getter和setter的实现
  11. //@synthesize 默认会去访问age,no,height同名的变量,,
  12. //如果找不到同名的变量,会在内部自动生成一个私有同名变量age,no,height,,
  13. //因此Student.h 中的这几个变量也可以省略不写。
  14. @synthesize age,no;

  15. @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. #import <Foundation/Foundation.h>
  9. #import "Student.h"

  10. int main(int argc, const char * argv[])
  11. {

  12. @autoreleasepool {

  13. // insert code here...
  14. Student *stu = [[Student alloc] init];
  15. stu.age = 100;
  16. NSLog(@"age is %i", stu.age);

  17. [stu release];
  18. }
  19. return 0;
  20. }



几点说明:

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

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

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