NSObject load 和 initialize 的调用时间和次数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
@interface Animal : NSObject  @end
@interface Cat : Animal @end
@interface Dog : Animal @end

@implementation Animal
+ (void)load {
NSLog(@"in Animal load %@",NSStringFromClass([self class]));
}
+(void) initialize{
NSLog(@"in Animal initialize with %@",NSStringFromClass([self class]));
}
@end

@implementation Cat @end
@implementation Dog @end

@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(@">1");
Cat *cat0 = [[Cat alloc] init];
NSLog(@">2");
Dog *dog0 = [[Dog alloc] init];
NSLog(@">3");
Cat *cat1 = [[Cat alloc] init];
NSLog(@">4");
Dog *dog1 = [[Dog alloc] init];
return YES;
}
@end

####console output

Animal initialize with Animal
1
2
3
4
5
6
7
in Animal load Animal
>1
in Animal initialize with Cat
>2
in Animal initialize with Dog
>3
>4