oc基礎複習08-OC的類方法

直接上代碼 代碼裏面有註釋spa

 1 #import <Foundation/Foundation.h>
 2 
 3 @interface Animal : NSObject
 4 //類方法使用 「+」開頭 不帶參數的
 5 +(void)eat;
 6 
 7 +(void)eat02:(NSString *)name;
 8 
 9 @end
10 
11 
12 @implementation Animal
13 
14 +(void)eat
15 {
16     NSLog(@"Animal is eating!");
17 };
18 
19 +(void)eat02:(NSString *)name
20 {
21     NSLog(@"Animal is eating %@", name);
22 }
23 
24 @end
25 
26 int main(int argc, const char * argv[]) {
27     @autoreleasepool {
28         // insert code here...
  //看到這裏沒有 直接使用類名調用方法
29 [Animal eat]; 30 [Animal eat02:@"leaf"]; 31 } 32 return 0; 33 }

 對象方法code

1.減號開頭對象

2.只能由對象來調用blog

3.對象方法能夠使用成員變量io

類方法:class

加號開頭 有類直接調用 類方法不能訪問成員變量(實例變量)效率

類方法的好處import

不依賴於對象 執行效率高變量

相關文章
相關標籤/搜索