2
ここでは、クラスBaseがあるとします。Objective-cクラスの作成メソッド
@inerface Base : NSObject
{
}
+(id) instance;
@end
@implementation Base
+(id) instance
{
return [[[self alloc] init] autorelease];
}
-(id) init
{
...
}
@end
私たちは派生クラスDerivedを持っています。
@interface Derived : Base
{
}
@end
これは、init
メソッドを再インプリメントします。
クラスメソッド+(id) instance
を使用してDerivedクラスのインスタンスを作成します。
id foo = [Derived instance];
これで、fooは実際にはBaseクラスです。
この場合、fooをDerivedクラスにするにはどうすればよいですか?
派生クラスのすべてのクラスメソッドを再実装する必要がありますか? (実際には、全く同じになります)。
もっとエレガントな方法がありますか?
私は戻り値[[[Base alloc] init] autorelease]を書きました。私のコードのインスタンスメソッドで。どうもありがとう! – Andrew
クラスメソッドで自己単語を使う方法を説明できますか?どういう意味ですか? – Andrew
"[メソッド]実装の中で、' self'と 'super'の両方が受信オブジェクトを参照しています。" [Language Summary](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual /ObjectiveC/Articles/ocLanguageSummary.html)を読んでください。 – zoul