Objective-c class create methods -
let's have class base.
@inerface base : nsobject { } +(id) instance; @end @implementation base +(id) instance { return [[[self alloc] init] autorelease]; } -(id) init { ... } @end and have derived class derived.
@interface derived : base { } @end which reimplements init method.
now want create instance of derived class using class method +(id) instance.
id foo = [derived instance]; and foo base class.
how achieve foo derived class in case?
should reimplement class method derived classes ? (actually immplementation totally same).
is there more elegant way ?
when create instance using [derived instance], instance of class derived. try it. trick messaging self in instance method:
+(id) instance { return [[[self alloc] init] autorelease]; } when send instance message base, self base. when send same message derived, self derived , therefore whole thing works desirable.
Comments
Post a Comment