Objective-C 中Self 和 Super 詳解本文要介紹的內容,在 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:] 時,開始做這幾個事:
構建 objc_super 的結構體,此時這個結構體的第一個成員變量 receiver 就是 PersonMe* me,和 self 相同。而第二個成員變量 superClass 就是指類 Person,因為 PersonMe 的超類就是這個 Person。