Super关键字的原理

2023-06-12  本文已影响0人  xxttw

specifies
/ˈspesɪfaɪz/ 指定

struct objc_super {
    __unsafe_unretained _Nonnull id receiver;       // 消息接收者
    __unsafe_unretained _Nonnull Class super_class; // 消息接收者的父类
};


- (void)run
{
    [super run];
    
struct __rw_objc_super arg = {self, class_getSuperclass(objc_getClass("WTStudent"))};
等价于
struct __rw_objc_super arg = {self, [WTPerson class] };
class_getSuperclass(objc_getClass("WTStudent")) 获取WTStudent类对象的父类
 objc_msgSendSuper(arg, @selector(run));
}
- (instancetype)init
{
    if (self = [super init]) {
        NSLog(@"[self class] = %@", [self class]);           // WTStudent
        NSLog(@"[self superclass] = %@", [self superclass]); //WTPerson
        NSLog(@"---------------------------------------------");
//        objc_msgSendSuper({self, [WTPerson Class]}, @selector(class));
        NSLog(@"[super class] = %@", [super class]);        // WTStudent
        NSLog(@"[super superclass] = %@", [super superclass]);  // WTPerson
    }
    return self;
}
2022-12-24 13:19:23.917196+0800 Super[13181:13076018] [self class] = WTStudent
2022-12-24 13:19:23.917404+0800 Super[13181:13076018] [self superclass] = WTPerson
2022-12-24 13:19:23.917422+0800 Super[13181:13076018] ---------------------------------------------
2022-12-24 13:19:23.917435+0800 Super[13181:13076018] [super class] = WTStudent
2022-12-24 13:19:23.917446+0800 Super[13181:13076018] [super superclass] = WTPerson
上一篇下一篇

猜你喜欢

热点阅读