在.NET中咱們靜態使用的關鍵字static有着舉足輕重的做用,static 方法能夠不用實例化類實例就能夠直接調用,static 屬性也是如此。在Object C中也存在static關鍵字,今天的學習過程使用到了這個關鍵字,在這裏記錄一下static的使用。html
在Object C的語法中聲明後的static靜態變量在其餘類中是不能經過類名直接訪問的,它的做用域只能是在聲明的這個.m文件中 。不過能夠調用這個類的方法間接的修改這個靜態變量的值。對於Object C中的類使用和定義在前面已經作過相應的記錄,能夠查看Object C學習筆記3-對象的使用和定義。ide
1. 靜態屬性學習
Object C中靜態屬性的定義和.NET中的有點不同,先看看以下代碼:測試
#import <Foundation/Foundation.h> @interface Person : NSObject { int age; NSString *name; static int totalCount; } @property int age; @property NSString *name; -(void) write; +(void) hello; @end
以上代碼定義 static int totalCount; 這樣在編譯器中編譯會報錯,這樣的代碼編寫對於編譯器是不承認的。正確的定義放入以下:spa
#import <Foundation/Foundation.h> @interface Person : NSObject { int age; NSString *name; } @property int age; @property NSString *name; -(void) write; +(void) hello; @end
#import "Person.h" static int count; @implementation Person @synthesize age; @synthesize name; -(void) write { NSLog(@"姓名:%@ 年齡:%i",name,age); } +(void) hello { count++; NSLog(@"static = %i",count); } @end
static 屬性應該定義在implementation中,並且寫在implementation以外或者方法之中。以上代碼是將static 屬性寫在implementation以外。code
@autoreleasepool { for(int i=0;i<5;i++){ [Person hello]; } } 測試結果 2014-02-15 22:03:14.642 Object11[488:303] static = 1 2014-02-15 22:03:14.644 Object11[488:303] static = 2 2014-02-15 22:03:14.644 Object11[488:303] static = 3 2014-02-15 22:03:14.645 Object11[488:303] static = 4 2014-02-15 22:03:14.645 Object11[488:303] static = 5
從以上代碼能夠看出,調用hello方法直接使用類名Person而非Person的實例,並且每次調用以後count都會+1.orm
2. static屬性在方法中htm
修改代碼以下,將static屬性改到方法中。對象
#import "Person.h" @implementation Person @synthesize age; @synthesize name; -(void) write { NSLog(@"姓名:%@ 年齡:%i",name,age); } +(void) hello { static int count; count++; NSLog(@"static = %i",count); }
使用如上方式和1中的效果同樣,static屬性是屬於類全局的,看看測試代碼就知道效果如何:blog
@autoreleasepool { for(int i=0;i<5;i++){ [Person hello]; } } //測試結果 2014-02-15 22:12:04.681 Object11[528:303] static = 1 2014-02-15 22:12:04.683 Object11[528:303] static = 2 2014-02-15 22:12:04.684 Object11[528:303] static = 3 2014-02-15 22:12:04.685 Object11[528:303] static = 4 2014-02-15 22:12:04.685 Object11[528:303] static = 5
效果和前面的是同樣的。下面再看看在實例方法中定義static 屬性看看效果以下,修改代碼以下:
#import "Person.h" @implementation Person @synthesize age; @synthesize name; -(void) write { static int myage=0; myage++; NSLog(@"年齡:%i",myage); } +(void) hello { static int count; count++; NSLog(@"static = %i",count); }
測試實例方法中的靜態屬性測試方法以下:
@autoreleasepool { for(int i=0;i<5;i++){ Person *p=[[Person alloc] init]; p.name=[NSString stringWithFormat:@" %@ %i",@"object c",i]; [p write]; } } //測試結果以下 2014-02-15 22:20:35.161 Object11[582:303] 姓名: object c 0 年齡:1 2014-02-15 22:20:35.163 Object11[582:303] 姓名: object c 1 年齡:2 2014-02-15 22:20:35.164 Object11[582:303] 姓名: object c 2 年齡:3 2014-02-15 22:20:35.164 Object11[582:303] 姓名: object c 3 年齡:4 2014-02-15 22:20:35.164 Object11[582:303] 姓名: object c 4 年齡:5
從以上測試代碼能夠看出,static 屬性定義到實例方法中一樣適用,在調用的循環過程當中for不斷實例化新的實例,name屬性在發生變化,而count保留上次運行的結果,這也就是static的做用。
3. 靜態方法
在.NET中定義靜態方法也須要適用static 關鍵字,可是在Object C並不是如此。在以前咱們定義方法的時候都是以下格式:
- (返回值類型) 方法名: 參數,...
適用靜態方法 就是將 "-" 改成 "+" 便可。
+(void) hello; 在interface中定義如上方法簽名,而在implementation中實現這個方法。
靜態方法的時候在上面的例子中已經提到過了,可詳細參考!