一.isa指針
-
上篇文章我們提到了實(shí)例對(duì)象芍躏,類對(duì)象和元類對(duì)象的存儲(chǔ)結(jié)構(gòu)里面都包含了一個(gè)isa指針降狠,今天我們來(lái)看看它的作用,以及實(shí)例對(duì)象類對(duì)象元類對(duì)象之間的關(guān)聯(lián)
-
實(shí)例對(duì)象的isa指針
- 當(dāng)實(shí)例對(duì)象(instance)調(diào)用對(duì)象方法的時(shí)候否纬,實(shí)例對(duì)象的isa指針指向類對(duì)象(class)蛋褥,在類對(duì)象中烙心,查找對(duì)象方法并調(diào)用
-
類對(duì)象的isa指針
-
類對(duì)象(class)的isa指針指向元類對(duì)象(meta-class),當(dāng)調(diào)用類方法時(shí)淫茵,類對(duì)象的isa指針指向元類對(duì)象痘昌,并在元類里面找到類方法并調(diào)用
-
二.類對(duì)象的superclass 指針
- 先兩個(gè)類,一個(gè)Person繼承自NSObject算灸,一個(gè)類繼承自Person
/// Person繼承自NSObject
@interface Person : NSObject
-(void)perMethod;
+(void)perEat;
@end
@implementation Person
-(void)perMethod{
}
+(void)perEat{
}
@end
/// student繼承自Person
@interface Student : Person
-(void)StudentMethod;
+(void)StudentEat;
@end
@implementation Student
-(void)StudentMethod{
}
+(void)StudentEat{
}
- 當(dāng)實(shí)例對(duì)象調(diào)用自身的對(duì)象方法時(shí)驻啤,它在自身的class對(duì)象中找到StudentMethod方法
Student *student = [[Student alloc]init];
[student StudentMethod]
- 當(dāng)實(shí)例對(duì)象調(diào)用父類的方法的時(shí)候
Student *student = [[Student alloc]init];
[student perMethod];
當(dāng)子類調(diào)用父類的實(shí)例方法的時(shí)候骑冗,子類的class類對(duì)象的superclass指針指向父類,直至基類(NSObject)找到方法并執(zhí)行(注意贼涩,這里指的是實(shí)例方法遥倦,也就是減號(hào)方法)
三.元類對(duì)象的superclass 指針
當(dāng)子類調(diào)用父類的類方法的時(shí)候,子類的superclass指向父類,并查找到相應(yīng)的類方法消略,調(diào)用
[Student perEat];
-
總的來(lái)說(shuō)瞎抛,isa,superclass的的關(guān)系可以用一副經(jīng)典的圖來(lái)表示