Objective-C使用動態綁定:即在運行時解析方法調用。動態綁定看起來像一個謊言
好。
而且use of dot notation really boils down to a method call
但是,爲什麼那麼,我不能做這樣的事情:
#import <Foundation/Foundation.h> int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; // Intercept the exception @try { @throw [ NSException exceptionWithName:@"Exception named ME!" reason:@"Because i wanted to" userInfo:nil ] ; } @catch(id exc) // pointer to an exception object? { //NSLog(@"%@ : %@\n", exc.name, exc.reason) ; // ILLEGAL: Request for member // 'name' in something not a structure or union.. // If objective-c uses dynamic binding, and dot notation // boils down to calling the getter, then // WHY do I have to cast to the concrete type here? // Only works if I cast to the concrete type NSException* NSException* nexc = (NSException*)exc ; NSLog(@"%@ : %@\n", nexc.name, nexc.reason) ; } [pool drain]; return 0; }
當我聽到「動態綁定」我想「,所以應該像一個腳本語言「,,我很驚訝Objective-C與JavaScript等腳本語言相比的靈活性。
你在混淆動態綁定與蛋糕。蛋糕是一個謊言。 – 2009-11-08 17:10:23
_「方法調用在運行時被解析」_實際上應該被認爲是「在運行時發生的消息調度」_。整個_dot操作符的用法比其他任何操作都更具破壞性。 – 2009-11-08 17:31:29
沒有什麼可以做的,它是具有一個附加約束的方法調用的非常明確的同義詞,該約束類型必須是衆所周知的。 (無論是否喜歡這個點或認爲這是一個很好的補充是完全的意見 - 沒有評論:)。 – bbum 2009-11-08 17:33:25