Then we combine the code to explore the Initialize and + load two method call timing, first of all is+ Load:#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 to self;} @end
#pragram--- Son class based on person---
#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
To run the program, let's look at the output log: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
This means that when I do not do anything with the class, the +load method is executed by default and is executed before the main function.
Let's take a look at+ Initializemethod, first create the person and Girl objects in 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]; Girl *c = [Girl new]; Girl *d = [Girl new];} @end
Let's take a look at the output log: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] main2015-10-27 15:33:56.259 initialize[11711:342410] +[ Person Initialize] person2015-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]
With this experiment we can determine two points:
- + Initialize method is similar to a lazy load, if you do not use this class, then the system will not call this method by default, and the default is only loaded once;
- + Initialize call occurs before the +init method.
Let's look at the next+ InitializeIn the relationship between the parent class and the child class, create a son class that inherits from the person class:#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
Check out the output log: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] Main2015-10-27 15:44:55.825 initialize[12024:351576] +[person Initialize] person2015-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] son2015-10-27 15:44:55.826 initialize[12024:351576]-[Person init]
We will find that the person class+ Initializemethod is called, but it is called by the subclass son, that is, when the subclass is created, the subclass calls the parent class's+ InitializeMethod.