f(sixleaves) = sixleaves

重剑无锋 大巧不工

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  95 随笔 :: 0 文章 :: 7 评论 :: 0 Trackbacks
main.m
 1 #import <Foundation/Foundation.h>
 2 #import "Person.h"
 3 #import "Student.h"
 4 int main(int argc, const char * argv[]) {
 5     
 6     // 类的加载在程序启动时候已经完成。
 7     
 8     // 类的初始化
 9     [[Student alloc] init];
10     [[Person alloc] init];
11     
12     
13     
14     /*
15      类的加载:
16      程序一启动就会自动加载全部的类,先加载父类,再加载子类。加载的时候
17      会自动调用类的类方法load。
18      
19      类的初始化:
20      第一次使用类的时候,只调用一次。(即可能是第一次使用子类时候、也可能是使用这个类的时候)
21      
22      总结:
23      类的加载和初始化顺序:
24      1.先加载父类再加载子类、接着加载分类。(程序已启动后,就加载全部类)
25      2.先初始化父类,再初始化子类。(程序第一次用到类时候,只调用一次)
26      (如果分类覆盖了父类或者子类中的initialize方法,那么只会调用分类的initialize)
27      */
28   
29     
30     return 0;
31 }
32 

输出:
1 2015-05-03 10:50:24.751 类的深入研究[2001:24795] Person+load
2 2015-05-03 10:50:24.752 类的深入研究[2001:24795] Student+load
3 2015-05-03 10:50:24.752 类的深入研究[2001:24795] Person(SWP)+load
4 2015-05-03 10:50:24.752 类的深入研究[2001:24795] Person(SWP)+initialize
5 2015-05-03 10:50:24.752 类的深入研究[2001:24795] Student+initialize
6 Program ended with exit code: 0

Person.m

 1 //
 2 //  Person.m
 3 //  类的深入研究
 4 //
 5 //  Created by sixleaves on 15/5/3.
 6 //  Copyright (c) 2015年 itcast. All rights reserved.
 7 //
 8 
 9 #import "Person.h"
10 
11 @implementation Person
12 + (void)test
13 {
14     NSLog(@"Person+test");
15 }
16 
17 // 在类被加载的时候调用
18 + (void)load
19 {
20     NSLog(@"Person+load");
21 }
22 
23 
24 + (void)initialize
25 {
26     NSLog(@"Person+initialize");
27 }
28 
29 @end
30 

Student.m

 1 #import "Student.h"
 2 
 3 @implementation Student
 4 + (void)load
 5 {
 6     NSLog(@"Student+load");
 7 }
 8 
 9 + (void)initialize
10 {
11     NSLog(@"Student+initialize");
12 }
13 @end
Person+SWP.m
 1 #import "Person+SWP.h"
 2 
 3 @implementation Person (SWP)
 4 + (void)load
 5 {
 6     NSLog(@"Person(SWP)+load");
 7 }
 8 
 9 + (void)initialize
10 {
11     NSLog(@"Person(SWP)+initialize");
12 }
13 @end
14 
posted on 2015-05-03 10:55 swp 阅读(107) 评论(0)  编辑 收藏 引用 所属分类: objective-c

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理