//------------------------Persion类----------------------------//
1 #import "Person.h"
2
3 @implementation Person
4 + (void)test
5 {
6 NSLog(@"调用了test方法");
7 }
8
9
10 // 当程序启动的时候,就会加载一次项目中所有的类。类加载完毕后就会调用+load方法
11 + (void)load
12 {
13 NSLog(@"Person---load");
14 }
15
16 // 当第一次使用这个类的时候,就会调用一次+initialize方法
17 + (void)initialize
18 {
19 NSLog(@"Person-initialize");
20 }
21
22 @end
//------------------------Student类------------------------//
1 #import <Foundation/Foundation.h>
2 #import "Person.h"
3
4 @interface Student : Person
5
6 @end
7
8
9 #import "Student.h"
10
11 @implementation Student
12
13 // 在类被加载的时候调用
14 + (void)load
15 {
16 NSLog(@"Student---load");
17 }
18
19
20 + (void)initialize
21 {
22 NSLog(@"Student-initialize");
23 }
24
25
26 @end
//-------------------------GoodStudent类-----------------------//
1 #import "Student.h"
2
3 @interface GoodStudent : Student
4
5 @end
6
7
8 #import "GoodStudent.h"
9
10 @implementation GoodStudent
11 + (void)load
12 {
13 NSLog(@"GoodStudent---load");
14 }
15
16
17 + (void)initialize
18 {
19 NSLog(@"GoodStudent-initialize");
20 }
21
22 @end
//---------------------Persion分类----------------------//
1 #import "Person.h"
2
3 @interface Person (MJ)
4
5 @end
6
7
8 #import "Person+MJ.h"
9
10 @implementation Person (MJ)
11 + (void)load
12 {
13 NSLog(@"Person(MJ)---load");
14 }
15 + (void)initialize
16 {
17 NSLog(@"Person(MJ)-initialize");
18 }
19 @end
//-------------------------------------测试文件---------------------------------------//
1 #import <Foundation/Foundation.h>
2 #import "Person.h"
3 #import "Student.h"
4 #import "GoodStudent.h"
5 /*
6 1.当程序启动时,就会加载项目中所有的类和分类,而且加载后会调用每个类和分类的+load方法。只会调用一次。
7
8 2.当第一次使用某个类时,就会调用当前类的+initialize方法
9
10 3.先加载父类,再加载子类(先调用父类的+load方法,再调用子类的+load方法)
11 先初始化父类,再初始化子类(先调用父类的+initialize方法,再调用子类的+initialize方法)
12 */
13
14 int main()
15 {
16 // [[GoodStudent alloc] init];
17
18 return 0;
19 }
20
21 void test1()
22 {
23 Person *p = [[Person alloc] init];
24
25 //[Person test];
26
27 // 内存中的类对象
28 // 类对象 == 类
29 Class c = [p class];
30 [c test];
31
32 Person *p2 = [[c new] init];
33
34
35 NSLog(@"00000");
36 }
37
38 void test()
39 {
40 // 利用Person这个类创建了2个Person类型的对象
41 Person *p = [[Person alloc] init];
42
43 Person *p2 = [[Person alloc] init];
44
45 Person *p3 = [[Person alloc] init];
46
47 // 获取内存中的类对象
48 Class c = [p class];
49
50 Class c2 = [p2 class];
51
52 // 获取内存中的类对象
53 Class c3 = [Person class];
54
55
56 NSLog(@"c=%p, c2=%p, c3=%p", c, c2, c3);
57
58 // 类本身也是一个对象,是个Class类型的对象,简称类对象
59
60 /*
61 利用Class 创建 Person类对象
62
63 利用 Person类对象 创建 Person类型的对象
64
65 */
66 }