[轉載]Objective-C 的 self 和 super 詳解

轉載自:http://web2.0coder.com/archives/305

 

在 Objective-C 中的類實現中經常看到這兩個關鍵字 ”self” 和 ”super”,以以前 oop 語言的經驗,拿 c++ 爲例,self 相當於 this,super 相當於調用父類的方法,這麼看起來是很容易理解的。以下面的代碼爲例:

@interface Person:NSObject {
    NSString*  name;
}
- (void) setName:(NSString*) yourName;
@end

@interface PersonMe:Person {
    NSUInteger age;
}
- (void) setAge:(NSUInteger) age;
- (void) setName:(NSString*) yourName andAge:(NSUInteger) age;
@end

@implementation PersonMe
- (void) setName:(NSString*) yourName andAge:(NSUInteger) age {
    [self setAge:age];
    [super setName:yourName];
}
@end

int main(int argc, char* argv[]) {
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]
    PersonMe* me = [[PersonMe alloc] init];
    [me setName:@"asdf" andAge:18];
    [me release];
    [pool drain];
    return 0;
}

    上面有簡單的兩個類,在子類PersonMe中調用了自己類中的setAge和父類中的setName,這些代碼看起來很好理解,沒什麼問題。
然後我在setName:andAge的方法中加入兩行:

NSLog(@"self ' class is %@", [self class]);
NSLog(@"super' class is %@", [super class]);

    這樣在調用時,會打出來這兩個的class,先猜下吧,會打印出什麼?按照以前oop語言的經驗,這裏應該會輸出:

self ' s class is PersonMe
super ' s class is Person

    但是編譯運行後,可以發現結果是:

self 's class is PersonMe
super ' s class is PersonMe

    self 的 class 和預想的一樣,怎麼 super 的 class 也是 PersonMe?

真相

    self 是類的隱藏的參數,指向當前當前調用方法的類,另一個隱藏參數是 _cmd,代表當前類方法的 selector。這裏只關注這個 self。super 是個啥?super 並不是隱藏的參數,它只是一個“編譯器指示符”,它和 self 指向的是相同的消息接收者,拿上面的代碼爲例,不論是用 [self setName] 還是 [super setName],接收“setName”這個消息的接收者都是 PersonMe* me 這個對象。不同的是,super 告訴編譯器,當調用 setName 的方法時,要去調用父類的方法,而不是本類裏的。

    當使用 self 調用方法時,會從當前類的方法列表中開始找,如果沒有,就從父類中再找;而當使用 super 時,則從父類的方法列表中開始找。然後調用父類的這個方法

One more step

    這種機制到底底層是如何實現的?其實當調用類方法的時候,編譯器會將方法調用轉成一個 C 函數方法調用,Apple 的 objcRuntimeRef 上說:

    Sending Messages

    When it encounters a method invocation, the compiler might generate a call to any of several functions to perform the actual message dispatch, depending on the receiver, the return value, and the arguments. You can use these functions to dynamically invoke methods from your own plain C code, or to use argument forms not permitted by NSObject’s perform… methods. These functions are declared in /usr/include/objc/objc-runtime.h.
    ■ objc_msgSend sends a message with a simple return value to an instance of a class.
    ■ objc_msgSend_stret sends a message with a data-structure return value to an instance of a class.
    ■ objc_msgSendSuper sends a message with a simple return value to the superclass of an instance of a class.
    ■ objc_msgSendSuper_stret sends a message with a data-structure return value to the superclass of an instance of a class.

    可以看到會轉成調用上面 4 個方法中的一個,由於 _stret 系列的和沒有 _stret 的那兩個類似,先只關注 objc_msgSend 和 objc_msgSendSuper 兩個方法。當使用 [self setName] 調用時,會使用 objc_msgSend 的函數,先看下 objc_msgSend 的函數定義:

id objc_msgSend(id theReceiver, SEL theSelector, ...)

    第一個參數是消息接收者,第二個參數是調用的具體類方法的 selector,後面是 selector 方法的可變參數。我們先不管這個可變參數,以 [self setName:] 爲例,編譯器會替換成調用 objc_msgSend 的函數調用,其中 theReceiver 是 self,theSelector 是 @selector(setName:),這個 selector 是從當前 self 的 class 的方法列表開始找的 setName,當找到後把對應的 selector 傳遞過去。

    而當使用 [super setName] 調用時,會使用 objc_msgSendSuper 函數,看下 objc_msgSendSuper 的函數定義:

id objc_msgSendSuper(struct objc_super *super, SEL op, ...)

    第一個參數是個objc_super的結構體,第二個參數還是類似上面的類方法的selector,先看下objc_super這個結構體是什麼東西:

struct objc_super {
    id receiver;
   Class superClass;
};

    可以看到這個結構體包含了兩個成員,一個是 receiver,這個類似上面 objc_msgSend 的第一個參數 receiver,第二個成員是記錄寫 super 這個類的父類是什麼,拿上面的代碼爲例,當編譯器遇到 PersonMe 裏 setName:andAge 方法裏的 [super setName:] 時,開始做這幾個事:

  1. 構建 objc_super 的結構體,此時這個結構體的第一個成員變量 receiver 就是 PersonMe* me,和 self 相同。而第二個成員變量 superClass 就是指類 Person,因爲 PersonMe 的超類就是這個 Person。
  2. 調用 objc_msgSendSuper 的方法,將這個結構體和 setName 的 sel 傳遞過去。函數裏面在做的事情類似這樣:從 objc_super 結構體指向的 superClass 的方法列表開始找 setName 的 selector,找到後再以 objc_super->receiver 去調用這個 selector,可能也會使用 objc_msgSend 這個函數,不過此時的第一個參數 theReceiver 就是 objc_super->receiver,第二個參數是從 objc_super->superClass 中找到的 selector

    裏面的調用機制大體就是這樣了,以上面的分析,回過頭來看開始的代碼,當輸出 [self class] 和 [super class] 時,是個怎樣的過程。

    當使用 [self class] 時,這時的 self 是 PersonMe,在使用 objc_msgSend 時,第一個參數是 receiver 也就是 self,也是 PersonMe* me 這個實例。第二個參數,要先找到 class 這個方法的 selector,先從 PersonMe 這個類開始找,沒有,然後到 PersonMe 的父類 Person 中去找,也沒有,再去 Person 的父類 NSObject 去找,一層一層向上找之後,在 NSObject 的類中發現這個 class 方法,而 NSObject 的這個 class 方法,就是返回 receiver 的類別,所以這裏輸出 PersonMe。

    當使用 [super class] 時,這時要轉換成 objc_msgSendSuper 的方法。先構造 objc_super 的結構體吧,第一個成員變量就是 self,第二個成員變量是 Person,然後要找 class 這個 selector,先去 superClass 也就是 Person 中去找,沒有,然後去 Person 的父類中去找,結果還是在 NSObject 中找到了。然後內部使用函數 objc_msgSend(objc_super->receiver, @selector(class))  去調用,此時已經和我們用 [self class] 調用時相同了,此時的 receiver 還是 PersonMe* me,所以這裏返回的也是 PersonMe。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章