我有繼承類B的類A,並且想實現類似下面的類。但是由於動態類型轉換,init方法獲得了遞歸調用。 有沒有辦法實現這樣的?任何建議? (無子類中改變「init的名字?多級繼承的客觀C [self init]層次
@interface A : NSObject
@property NSData * data;
@end
@implementation A
- (id) init {
self = [super init];
/* want to do some default initialization here
for this class and all of its subclasses. */
// ... ...
return self;
}
/* This [self init] get recursed.
But how can I force this class and all of its subclass to call [self init] above,
not to call the subclass's init method. */
- (id) initWithData:(NSData *)d {
self = [self init];
self.data = d;
return self;
}
@end
@interface B : A
@end
#import "B.h"
@implementation B
- (id) init {
self = [super initWithData:nil];
// some subclass specific init code here ...
return
}
@end
使用B,
- (void) testInit{
B * b = [[B alloc] init];
}
「類似下面」 - 像什麼? 「由於動態類型轉換,init方法獲得遞歸調用」 - ??? – 2013-10-28 13:01:12