oc40--类的启动过程
生活随笔
收集整理的這篇文章主要介紹了
oc40--类的启动过程
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
//
// main.m
// 類的啟動過程#import <Foundation/Foundation.h>
#import "Person.h"
#import "GoodStudent.h"/* 只要程序啟動就會將所有類的代碼加載到內存中, 放到代碼區load方法會在當前類被加載到內存的時候調用, 有且僅會調用一次如果存在繼承關系, 會先調用父類的load方法, 再調用子類的load方法*/int main(int argc, const char * argv[]) {/*2017-08-23 15:05:08.882568+0800 類的啟動過程[36713:860727] Car類被加載到內存了2017-08-23 15:05:16.019104+0800 類的啟動過程[36713:860727] Person類被加載到內存了2017-08-23 15:05:17.390692+0800 類的啟動過程[36713:860727] Student類被加載到內存了2017-08-23 15:05:19.161607+0800 類的啟動過程[36713:860727] GoodStudent類被加載到內存了還沒有使用類的時候就加載到內存了。*/Person *p1 = [[Person alloc] init];Person *p2 = [[Person alloc] init];Person *p3 = [[Person alloc] init];Person *p4 = [[Person alloc] init];GoodStudent *gstu = [[GoodStudent alloc] init];/*2017-08-23 15:08:55.461633+0800 類的啟動過程[36844:863122] Person initialize2017-08-23 15:09:01.543395+0800 類的啟動過程[36844:863122] Studnet initialize2017-08-23 15:09:06.376914+0800 類的啟動過程[36844:863122] GoodStudent initialize */return 0;
} //
// Person.h#import <Foundation/Foundation.h>@interface Person : NSObject
@end //
// Person.m#import "Person.h"@implementation Person
// 只要程序啟動就會將所有類的代碼加載到內存中, 放到代碼區
// load方法會在當前類被加載到內存的時候調用, 有且僅會調用一次
// 如果存在繼承關系, 會先調用父類的load方法, 再調用子類的load方法
+ (void)load
{NSLog(@"Person類被加載到內存了");
}// 當當前類第一次被使用的時候就會調用(創建類對象的時候)
// initialize方法在整個程序的運行過程中只會被調用一次, 無論你使用多少次這個類都只會調用一次
// initialize用于對某一個類進行一次性的初始化
// initialize和load一樣, 如果存在繼承關系, 會先調用父類的initialize再調用子類的initialize
+ (void)initialize
{NSLog(@"Person initialize");
}@end //
// Student.h#import "Person.h"@interface Student : Person@end //
// Student.m#import "Student.h"@implementation Student
+ (void)load
{NSLog(@"Student類被加載到內存了");
}
+ (void)initialize
{NSLog(@"Studnet initialize");
}
@end //
// GoodStudent.h#import "Student.h"@interface GoodStudent : Student@end //
// GoodStudent.m#import "GoodStudent.h"@implementation GoodStudent
+ (void)load
{NSLog(@"GoodStudent類被加載到內存了");
}
+ (void)initialize
{NSLog(@"GoodStudent initialize");
}@end //
// Car.h#import <Foundation/Foundation.h>@interface Car : NSObject@end //
// Car.m#import "Car.h"@implementation Car
+ (void)load
{NSLog(@"Car類被加載到內存了");
}
+ (void)initialize
{NSLog(@"Car initialize");
}
@end
?
轉載于:https://www.cnblogs.com/yaowen/p/7418320.html
總結
以上是生活随笔為你收集整理的oc40--类的启动过程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于node-sass安装失败的解决办法
- 下一篇: 小程序使用wxParse解析html