-1

次のインターフェースを検討してください。

@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
4

1 に答える 1

1

を使用する場合super、親クラスを参照していません。ある意味で、作成しているオブジェクトを実際に参照しています。この質問を参照してください:

Objective-C のスーパーとは正確には何ですか?

回答には、実際に詳細に説明する多くのリンクと情報があります。

于 2012-08-31T17:44:00.350 に答える