Objective c 什么';[super init]和[[ParentClass alloc]init]之间的区别是什么?
考虑以下接口:Objective c 什么';[super init]和[[ParentClass alloc]init]之间的区别是什么?,objective-c,ios,oop,inheritance,Objective C,Ios,Oop,Inheritance,考虑以下接口: @interface Bar : NSObject @end @interface Foo : Bar @end 这两者有什么区别 @implementation Foo - (id)init { return [super init]; } @end 。。。而且 @implementation Foo - (id)init { return [[Bar alloc] init]; } @end 编辑:为了澄清,我添加了更多代码 /* ===
@interface Bar : NSObject
@end
@interface Foo : Bar
@end
这两者有什么区别
@implementation Foo
- (id)init
{
return [super init];
}
@end
。。。而且
@implementation Foo
- (id)init
{
return [[Bar alloc] init];
}
@end
编辑:为了澄清,我添加了更多代码
/* =============
* GenericCell.h
* =============
*/
// imports, etc.
typedef enum {
GenericCellStyleFoo,
GenericCellStyleBar
} GenericCellStyle;
@interface GenericCell : UITableViewCell
- (id)initWithStyle:(GenericCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier;
@end
/* =============
* GenericCell.m
* =============
*/
// imports, etc.
@interface FooCell : GenericCell
- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier;
@end
@interface BarCell : GenericCell
- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier;
@end
@implementation FooCell
- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
// *************************************************************************
// This will actually call GenericCell's 'initWithStyle:reuseIdentifier:',
// hence the "loop"!
// *************************************************************************
self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
if (self) {
// various subviews or whatever to make this cell so special...
}
return self;
}
@end
@implementation BarCell
- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
// *************************************************************************
// This will actually call GenericCell's 'initWithStyle:reuseIdentifier:',
// hence the "loop"!
// *************************************************************************
self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
if (self) {
// various subviews or whatever to make this cell so special...
}
return self;
}
@end
@implementation GenericCell
// some useful code...
- (id)initWithStyle:(GenericCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (style == GenericCellStyleFoo) {
return [[FooCell alloc] initWithReuseIdentifier:reuseIdentifier];
}
else if (style == GenericCellStyleBar) {
return [[BarCell alloc] initWithReuseIdentifier:reuseIdentifier];
}
return nil; // doesn't really matter for sake of this example
}
// some useful code...
@end
/* =================================
* SomeTableViewControllerInstance.m
* =================================
*/
// imports, etc.
@implementation SomeTableViewControllerInstance
// some useful code...
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
GenericCellStyle style = [self styleDoesntMatterHowForRowAtIndexPath:indexPath];
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell;
// dequeue and all that...
if (cell == nil) {
cell = [[GenericCell alloc] initWithStyle:style reuseIdentifier:CellIdentifier];
}
// set labels, or whatever...
return cell;
}
// some useful code...
@end
// End of file
当您使用
super
时,您并没有引用父类。在某种程度上,您实际上是指您正在创建的对象。见这个问题:
答案中有大量的链接和信息,它们确实详细地解释了问题。您在哪里看到第二种方法的使用?它似乎没有什么用处。我遇到的情况是,如果有人调用类Foo的init,在内部我可能会在不同的情况下创建一个类型为“FooSomething:Foo”或“FooSomething:Foo”的对象。由于Foo*和Foo都有“init”,如果在其中一个Foo*中执行[super init],则会使自己陷入一个循环中。一方面,第二个会泄漏。主要是,第一个会返回一个Foo对象,而第二个会返回一个Bar对象(并泄漏创建的Foo)。而不是调用
alloc/initWithStyle
,调用GenericCell+newGenericCellFactory
,并将所需的逻辑放入其中。如果必须中断循环,则仍必须调用“self=[[ParentClass alloc]init]”是否有任何错误?我知道“[super init]”是个不错的选择,但我似乎无法打破它。我想你可以。我不知道你为什么要这么做。这篇文章有点有趣,但请注意底部的更新:我认为我不理解您使用[super init]
的问题。为什么它会根据情况返回不同的对象类型?你能从你的类中发布一些代码吗?@highed--你想“打破”的“循环”是什么?@HotLicks我希望我的新编辑能把事情弄清楚一点。