class superclass

2018-09-05  本文已影响1人  曹来东

下面代码打印结果

@interface LDPerson : NSObject
@end
@interface LDStudent : LDPerson
@end
@implementation LDStudent
- (instancetype)init{
    if (self = [super init]) {
        
        NSLog(@"[self class] %@",[self class]);//LDStudent
        NSLog(@"[self superclass] %@",[self superclass]);//LDPerson
        NSLog(@"[super class] %@",[super class]);////LDStudent
        NSLog(@"[super superclass] %@",[super superclass]);//LDPerson
    }
    return self;
}
@end

底层代码

1. objc_msgSend(self, @selector(class));//[self class]
2. objc_msgSend(self, @selector("superclass"));//[self superclass]
3. objc_msgSendSuper(self, [LDPerson class]), @selector(class)));//[super class]
4. objc_msgSendSuper(self, [LDPerson class]), @selector("superclass")));//[super superclass]

单从消息机制层面来说:objc_msgSend(receiver,SEL).只要receiver和SEL相同,结果就是相同的.所以1,3结果相同;2,4结果相同;

[self class] 和 [super class]

  1. 首先看objc_msgSend(self, @selector(class)).在self(LDStudent class对象)的缓存方法列表中,方法列表中寻找该方法如果没有通过superClass指针去父类中查找.因为classNSObject中的方法.所以在NSObject中调用class方法.
class方法实现
+ (Class)class {
    return self;
}

- (Class)class {
//传入instance对象,获取class对象
//传入class对象,获取meta-class对象
    return object_getClass(self);
}
Class object_getClass(id obj)
{
    if (obj) return obj->getIsa();
    else return Nil;
}

所以[self class]返回的是LDStudent的class对象.
2.再看[super class]objc_msgSendSuper(self, [LDPerson class]), @selector(class)))方法中的第二个参数[LDPerson class]表示,从哪里开始寻找class方法.上面objc_msgSend(self, @selector(class));//[self class]表示从LDStudent class对象开始寻找.此时表示从LDStudent的父类LDPerson开始寻找该方法.因为在LDStudentLDPerson类中都没有class方法的实现.所以不会影响打印结果.即执行的都是NSObjectclass方法,因为NSObjectclass方法实现相同,而这两次方法的调用者即receiver都相同,即class方法中传入的对象都相同,所以打印结果相同.

再看[self superclass] 和[super superclass]

+ (Class)superclass {
    return self->superclass;
}

- (Class)superclass {
    return [self class]->superclass;
}

从另外一个角度看

1. objc_msgSend(self, @selector(class));//[self class]
2. objc_msgSend(self, @selector("superclass"));//[self superclass]
3. objc_msgSendSuper(self, [LDPerson class]), @selector(class)));//[super class]
4. objc_msgSendSuper(self, [LDPerson class]), @selector("superclass")));//[super superclass]

[super class],[super superclass][self class],[self superclass]想比:

objc4源码

+ (id)self {
    return (id)self;
}

- (id)self {
    return self;
}

+ (Class)class {
    return self;
}

- (Class)class {
    return object_getClass(self);
}

+ (Class)superclass {
    return self->superclass;
}

- (Class)superclass {
    return [self class]->superclass;
}

+ (BOOL)isMemberOfClass:(Class)cls {
    return object_getClass((id)self) == cls;
}

- (BOOL)isMemberOfClass:(Class)cls {
    return [self class] == cls;
}

+ (BOOL)isKindOfClass:(Class)cls {
    for (Class tcls = object_getClass((id)self); tcls; tcls = tcls->superclass) {
        if (tcls == cls) return YES;
    }
    return NO;
}

- (BOOL)isKindOfClass:(Class)cls {
    for (Class tcls = [self class]; tcls; tcls = tcls->superclass) {
        if (tcls == cls) return YES;
    }
    return NO;
}

+ (BOOL)isSubclassOfClass:(Class)cls {
    for (Class tcls = self; tcls; tcls = tcls->superclass) {
        if (tcls == cls) return YES;
    }
    return NO;
}
上一篇下一篇

猜你喜欢

热点阅读