[super init]和[[ParentClass alloc] init]之间有什么区别?

时间:2012-08-31 17:24:16

标签: 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

...

编辑澄清我正在添加更多代码......

/* =============
 * 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

1 个答案:

答案 0 :(得分:1)

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

What exactly is super in Objective-C?

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

相关问题