2
假設我們有一個類Base。Objective-c類創建方法
@inerface Base : NSObject
{
}
+(id) instance;
@end
@implementation Base
+(id) instance
{
return [[[self alloc] init] autorelease];
}
-(id) init
{
...
}
@end
而且我們派生了派生類。
@interface Derived : Base
{
}
@end
其中重新實現了init
方法。
現在我們要使用類方法+(id) instance
創建派生類的實例。
id foo = [Derived instance];
現在foo實際上是一個基類。
如何在這種情況下實現foo作爲派生類?
我應該重新實現派生類的所有類方法嗎? (實際上不會完全相同)。
有沒有更優雅的方式?
該死,我寫過return [[[Base alloc] init] autorelease];在我的代碼中的實例方法。非常感謝! – Andrew
你能解釋一下如何在類方法中使用自我單詞?這是什麼意思? – Andrew
「在[method]實現中,'self'和'super'都指向接收對象,」請參見[Language Summary](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual /ObjectiveC/Articles/ocLanguageSummary.html)在Apple的Objective-C書中。 – zoul