首先代码展示吧,后面也会附赠demo代码链接
1.定义测试类TestClass
//为了方便打印,先在宏定义文件里面定义ZWWLog
#ifdef DEBUG
#define ZWWLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
#define ZWWLog(...)
#endif
TestClass.m
+ (void)load{
ZWWLog();
}
+ (void)initialize
{
ZWWLog(@"TestClass 中的initialize方法执行 class:%@",[self class]);
}
- (instancetype)init{
ZWWLog(@"TestClass 中的init方法执行 class:%@",[self class]);
return [super init];
}
2.定义测试类TestClass的子类TestClassSon
TestClassSon.m
+ (void)load{
ZWWLog();
}
+ (void)initialize
{
ZWWLog();
}
- (instancetype)init
{
ZWWLog();
return [super init];
}
2.定义测试类TestClass的分类1TestClass (Method)
//不会覆盖主类的load
+ (void)load{
ZWWLog();
}
//会覆盖主类的initialize
//+ (void)initialize
//{
// if (self == [self class]) {
// ZWWLog();
// }
//}
//会覆盖主类的init方法
//- (instancetype)init
//{
// ZWWLog(@"TestClass (Method) init方法执行");
// self = [super init];
// if (self) {
//
// }
// return self;
//}
2.定义测试类TestClass的分类2TestClass (Method1)
+ (void)load{
ZWWLog();
}
//+ (void)initialize
//{
// if (self == [self class]) {
// ZWWLog();
// }
//}
//- (instancetype)init
//{
// ZWWLog(@"TestClass (Method1) init方法执行");
// self = [super init];
// if (self) {
//
// }
// return self;
//}
好了,代码准备完毕,下面在控制器中调用类,根据打印结果总结结论:
在控制器中需要导入对应头文件
#import "TestClass.h"
#import "TestClassSon.h"
#import "TestClass+Method.h"
#import "TestClass+method1.h"
1.load方法测试:什么操作也不做,直接commond+b编译,打印如下:
2018-10-04 14:47:17.766168+0800 InterviewDemo[4064:619260] +[TestClass load] [Line 26]
2018-10-04 14:47:17.766824+0800 InterviewDemo[4064:619260] +[TestClassSon load] [Line 14]
2018-10-04 14:47:17.778301+0800 InterviewDemo[4064:619260] +[TestClass(Method) load] [Line 28]
2018-10-04 14:47:17.778399+0800 InterviewDemo[4064:619260] +[TestClass(method1) load] [Line 13]