美文网首页
Super关键字的原理

Super关键字的原理

作者: xxttw | 来源:发表于2023-06-12 00:12 被阅读0次

    specifies
    /ˈspesɪfaɪz/ 指定

    • super 指的是从父类中开始查找方法, 实际调用还是当前消息接收者
    • objc_msgSendSuper(self, [WTPerson class], @selector(run));
    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
    

    相关文章

      网友评论

          本文标题:Super关键字的原理

          本文链接:https://www.haomeiwen.com/subject/kibbqdtx.html