Objective-C 的 self 和 super 詳解

huwen1232002發表於2012-03-23

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

        在 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。

轉自 http://web2.0coder.com/archives/3

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26749246/viewspace-719323/,如需轉載,請註明出處,否則將追究法律責任。

相關文章