1 #import <Foundation/Foundation.h>
2
3 @interface Person : NSObject
4 + (void)test;
5 - (void)test;
6 @end
7
8 @implementation Person
9 + (void)test
10 {
11 NSLog(@"Class Method test");
12 }
13 - (void)test
14 {
15 NSLog(@"Instance Method test");
16 }
17
18 - (void)fuck
19 {
20 NSLog(@"Instance Method fuck");
21 }
22 @end
23
24 int main() {
25
26 Person *p = [Person new];
27 //[p test]; unrecognized selector sent to instance 0x7f9c11c10600
28 [Person test];
29
30 // [Person fuck]; unrecognized selector sent to class 0x10a1f71c0
31 [p fuck];
32 return 0;
33 }
34
35 // 總結OC中類方法與對象方法的區別
36 /*
37 1.對象方法一減號開頭。類方法以加號開頭
38 2.對象方法只能有對象調用。類方法只能由類調用,否則運行時候程序會異常退出。
39 3.類方法不依賴于對象,所以其不能訪問成員變量。
40 Tips:對象方法與類方法能同名存在。
41 */