/**load * 當類一加載到OC運行時系統中(內存),就會調用一次 * 程序一塊兒的就會調用 * 程序運行過程當中,只會調用一次 ** 若是某個類的某個方法永遠都只執行一次,那就放入load方法中最合適 */ /**initialize * 當第一次使用這個類的時候纔會調用 * 並不是程序一啓動就會調用 */
#import "Person.h" @implementation Person + (void)load { NSLog(@"------------person load--------------"); } + (void)initialize { NSLog(@"------------person initialize--------------"); } #import "Child.h" @implementation Child + (void)load { NSLog(@"------------Child load--------------"); } + (void)initialize { NSLog(@"------------Child initialize--------------"); } #import "ViewController.h" #import "Person.h" #import "Child.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //Person *p = [[Person alloc] init]; Child *c = [[Child alloc] init]; } 輸出結果 2015-07-19 17:05:59.735 test[27283:1314612] ------------person load-------- 2015-07-19 17:05:59.739 test[27283:1314612] ------------Child load---------- 2015-07-19 17:05:59.928 test[27283:1314612] ------------person initialize--- 2015-07-19 17:13:05.182 test[27396:1319499] ------------Child initialize---- ps:若是子類沒有initialize方法,就會載入父類 輸出結果:父類initialize重複調用 2015-07-19 17:05:59.735 test[27283:1314612] ------------person load-------- 2015-07-19 17:05:59.739 test[27283:1314612] ------------Child load---------- 2015-07-19 17:05:59.928 test[27283:1314612] ------------person initialize--- 2015-07-19 17:13:05.182 test[27396:1319499] ------------person initialize----