标签:
Objective-C 有两个神奇的方法:+load 和 +initialize,这两个方法在类被使用时会自动调用。但是两个方法的不同点会导致应用层面上性能的显著差异。
#pragram ---main函数中的代码---
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char * argv[]) {
NSLog(@"%s",__func__);
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
#pragram ---基于NSObject的Person类---
#import "Person.h"
@implementation Person
+ (void)load{
NSLog(@"%s",__func__);
}
+ (void)initialize{
[super initialize];
NSLog(@"%s %@",__func__,[self class]);
}
- (instancetype)init{
if (self = [super init]) {
NSLog(@"%s",__func__);
}
return self;
}
@end
#pragram ---基于Person的Son类---
#import "Girl.h"
@implementation Girl
+ (void)load{
NSLog(@"%s ",__func__);
}
+ (void)initialize{
[super initialize];
NSLog(@"%s ",__func__);
}
- (instancetype)init{
if (self = [super init]) {
NSLog(@"%s",__func__);
}
return self;
}
@end
运行程序,我们看一下输出日志:
2015-10-27 15:21:07.545 initialize[11637:334237] +[Person load]
2015-10-27 15:21:07.546 initialize[11637:334237] +[Girl load]
2015-10-27 15:21:07.546 initialize[11637:334237] main
这说明在我并没有对类做任何操作的情况下,+load 方法会被默认执行,并且是在 main 函数之前执行的。#import "ViewController.h"
#import "Person.h"
#import "Son.h"
#import "Girl.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
Person * a = [Person new];
Person * b = [Person new];
Girl *c = [Girl new];
Girl *d = [Girl new];
}
@end
下面我们来看一下输出日志:
2015-10-27 15:33:56.195 initialize[11711:342410] +[Person load]
2015-10-27 15:33:56.196 initialize[11711:342410] +[Girl load]
2015-10-27 15:33:56.197 initialize[11711:342410] main
2015-10-27 15:33:56.259 initialize[11711:342410] +[Person initialize] Person
2015-10-27 15:33:56.259 initialize[11711:342410] -[Person init]
2015-10-27 15:33:56.259 initialize[11711:342410] -[Person init]
2015-10-27 15:33:56.259 initialize[11711:342410] +[Girl initialize]
2015-10-27 15:33:56.260 initialize[11711:342410] -[Girl init]
2015-10-27 15:33:56.260 initialize[11711:342410] -[Girl init]
通过这个实验我们可以确定两点:
#pragram ---ViewController 中的代码---
#import "ViewController.h"
#import "Person.h"
#import "Son.h"
#import "Girl.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
Person * a = [Person new];
Person * b = [Person new];
Son*z = [Son new];
}
@end
看一下输出日志:
2015-10-27 15:44:55.762 initialize[12024:351576] +[Person load]
2015-10-27 15:44:55.764 initialize[12024:351576] +[Son load]
2015-10-27 15:44:55.764 initialize[12024:351576] +[Girl load]
2015-10-27 15:44:55.764 initialize[12024:351576] main
2015-10-27 15:44:55.825 initialize[12024:351576] +[Person initialize] Person
2015-10-27 15:44:55.825 initialize[12024:351576] -[Person init]
2015-10-27 15:44:55.825 initialize[12024:351576] -[Person init]
2015-10-27 15:44:55.826 initialize[12024:351576] +[Person initialize] Son
2015-10-27 15:44:55.826 initialize[12024:351576] -[Person init]
我们会发现 Person 类的 + initialize 方法又被调用了,但是查看一下是子类 Son 调用的,也就是创建子类的时候,子类会去调用父类的 + initialize 方法。
标签:
原文地址:http://www.cnblogs.com/weiboyuan/p/5691074.html