ios的@property屬性和@synthesize屬性(轉)

來源:互聯網
上載者:User

標籤:c   style   class   blog   code   tar   

 

當你定義了一系列的變數時,需要寫很多的getter和setter方法,而且它們的形式都是差不多的,,所以Xcode提供了@property 和@synthesize屬性,@property用在 .h 標頭檔中用作聲明,@synthesize用在.m 檔案中用於實現。

如下,建立一個基於“Command Line Tool”的項目,名為“property”,再建立一個Student類,

傳統的寫法是:

Student.h

[cpp] view plaincopy
  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

[cpp] view plaincopy
  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

[cpp] view plaincopy
  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

[cpp] view plaincopy
  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

[cpp] view plaincopy
  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

[cpp] view plaincopy
  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

 

摘自:http://blog.csdn.net/chaoyuan899/article/details/10310719

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.