【问题标题】:What's the difference between [super init] and [[ParentClass alloc] init]? [closed][super init] 和 [[ParentClass alloc] init] 有什么区别? [关闭]
【发布时间】:2012-08-31 17:24:16
【问题描述】:

考虑以下接口:

@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

【问题讨论】:

  • 您在哪里看到使用的第二种方式?好像没什么用。
  • 我的情况是,如果有人调用 Foo 类的 init,在内部我可能会在不同的情况下创建一个类型为“FooSomething : Foo”或“FooSomeOtherThing : Foo”的对象。由于 Foo* 和 Foo 都有“init”,如果你在其中一个 Foo* 中执行 [super init],你就会让自己陷入循环。
  • 一方面,你在第二个泄漏。
  • 主要是第一个会返回一个Foo对象,第二个会返回一个Bar对象(并泄漏创建的Foo)。
  • 不要调用alloc/initWithStyle,而是调用GenericCell +newGenericCellFactory,并把你想要的逻辑放在那里。

标签: objective-c ios oop inheritance


【解决方案1】:

当您使用super 时,您指的不是父类。在某种程度上,您实际上指的是您正在创建的对象。看到这个问题:

What exactly is super in Objective-C?

答案中有很多链接和信息,可以真正详细地解释它。

【讨论】:

  • 如果我必须打破循环,仍然必须调用“self = [[ParentClass alloc] init]”,有什么问题吗?我知道“[super init]”是要走的路,但我似乎无法打破它。
  • 我猜你可以。我不知道你为什么会。这篇文章有点意思,但请注意底部的更新:wilshipley.com/blog/2005/07/self-stupid-init.html
  • 我认为我不理解您在使用 [super init] 时遇到的问题。为什么它会根据情况返回不同的对象类型?你能从你的课程中发布一些代码吗?
  • @exalted -- 你想“打破”这个“循环”是什么?
  • @HotLicks 我希望我的新编辑能让事情变得更清晰。
猜你喜欢
  • 1970-01-01
  • 2016-02-18
  • 2013-06-12
  • 2012-02-08
  • 1970-01-01
  • 2018-11-05
  • 2016-01-22
  • 1970-01-01
  • 2012-06-30
相关资源
最近更新 更多