- super调用,底层会转换为objc_msgSendSuper2函数的调用,接收2个参数,struct objc_super2 和 SEL
struct objc_super2{
id receiver; // 消息接收者
Class current_class; // receiver 的 Class 对象
}
- super 调用其本质还是 self,只不过用它调用方法时是从父类的方法列表中开始找
@interface Person : NSObject
@end
@implementation Person
@end
@interface Student : Person
@end
@implementation Student
- (instancetype)init{
if (self = [super init]) {
NSLog(@"[self class] = %@", [self class]); // Student
NSLog(@"[self superclass] = %@", [self superclass]); // Person
NSLog(@"[super class] = %@", [super class]); // Student
NSLog(@"[super superclass] = %@", [super superclass]);// Person
}
return self;
}
@end
对于上面[super superclass]
显示的是 Person,是这样的 superclass
方法的调用者是 super,本质还是 Student 对象,不过要从Student 的父类 Person 中去找superclass
方法,但调用者还是 Student 对象,Student 对象调用 superClass 方法返回的自然是 Person.
网友评论