【发布时间】: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