【问题标题】:Create subclass with different subclass property创建具有不同子类属性的子类
【发布时间】:2012-05-26 23:37:55
【问题描述】:

假设我有这门课

@interface CustomClass : NSObject

@property (nonatomic, strong) NSArray * nicestArrayEver;

@end

我想创建一个 CustomClass 的子类,但这里有个问题

@interface ASubClassCustomClass : CustomClass

@property (nonatomic, strong) NSMutableArray * nicestArrayEver;

@end

您可以想象的问题是,当我初始化 ASubClassCustomClass 并调用它的超级初始化程序(因为需要其他属性)时,创建了不可变的 nicestArrayEver。我怎样才能避免它的创建,以便我可以设置可变的?

注意:这只是一个例子,真正的实现调用了一个重创建和真正定制的子类(不是一个NSArray)。

【问题讨论】:

    标签: objective-c cocoa subclass


    【解决方案1】:

    您可以通过使用不同的支持变量使其工作,合成时看起来像这样:@synthesize nicestArrayEver = nicestArrayEverSubClass_;

    #import <Foundation/Foundation.h>
    
    @interface CustomClass : NSObject
    
    @property (nonatomic, strong) NSArray * nicestArrayEver;
    
    @end
    
    @implementation CustomClass
    @synthesize nicestArrayEver ;
    
    -(id)init
    {
        if (self = [super init]) {
            nicestArrayEver = [[NSArray alloc] init];
        }
        return self;
    }
    @end
    
    @interface ASubClassCustomClass : CustomClass
    
    @property (nonatomic, strong) NSMutableArray * nicestArrayEver;
    
    @end
    
    @implementation ASubClassCustomClass
    @synthesize nicestArrayEver = nicestArrayEverSubClass_;
    
    -(id)init{
        if (self = [super init]) {
            nicestArrayEverSubClass_ = [[NSMutableArray alloc] init];
        }
        return self;
    }
    @end
    
    
    
    int main(int argc, const char * argv[])
    {
    
        @autoreleasepool {
    
            CustomClass *c1 = [[[CustomClass alloc] init] autorelease];
            ASubClassCustomClass *c2 = [[[ASubClassCustomClass alloc] init] autorelease];
    
            NSLog(@"%@", NSStringFromClass([[c1 nicestArrayEver] class]));
            NSLog(@"%@", NSStringFromClass([[c2 nicestArrayEver] class]));
    
        }
        return 0;
    }
    

    输出

    2012-05-27 01:59:16.221 NicestArray[2312:403] __NSArrayI
    2012-05-27 01:59:16.225 NicestArray[2312:403] __NSArrayM
    

    另一种方法是在基类中设置 2 个 init 方法,一个用于实例化属性,一个不实例化,但将任务留给子类 — 这将阻止您创建昂贵的对象把它们扔掉。
    现在,基类可以直接使用第二个 init 实例化并进入错误状态。您可以通过使用isMemberOfClass: 检查 self 类类型来避免这种情况,如果类类型是基类,则会引发错误。

    @interface CustomClass : NSObject
    
    @property (nonatomic, strong) NSArray * nicestArrayEver;
    -(id)initWithoutArray;
    @end
    
    @implementation CustomClass
    @synthesize nicestArrayEver ;
    
    -(id) initWithoutArray
    {
        if (self = [super init]) {
            if ([self isMemberOfClass:[CustomClass class]]) {
                [NSException raise:@"AbstractMethodCall" format:@"%@ should be called only from Subclasses of %@", NSStringFromSelector(_cmd), NSStringFromClass([self class])];
            }
        }
        return self;
    }
    
    
    -(id)init
    {
        if (self = [super init]) {
            nicestArrayEver = [[NSArray alloc] init];
        }
        return self;
    }
    @end
    
    @interface ASubClassCustomClass : CustomClass
    
    @property (nonatomic, strong) NSMutableArray * nicestArrayEver;
    
    @end
    
    @implementation ASubClassCustomClass
    @synthesize nicestArrayEver = nicestArrayEverSubClass_;
    
    -(id)init{
        if (self = [super initWithoutArray]) {
            nicestArrayEverSubClass_ = [[NSMutableArray alloc] init];
        }
        return self;
    }
    
    @end
    
    
    
    int main(int argc, const char * argv[])
    {
    
        @autoreleasepool {
    
            CustomClass *c1 = [[[CustomClass alloc] init] autorelease];
            ASubClassCustomClass *c2 = [[[ASubClassCustomClass alloc] init] autorelease];
    
            NSLog(@"%@", NSStringFromClass([[c1 nicestArrayEver] class]));
            NSLog(@"%@", NSStringFromClass([[c2 nicestArrayEver] class]));
    
            //this works, as it is the subclass
            ASubClassCustomClass *shouldWork = [[[ASubClassCustomClass alloc] init] autorelease];
    
            // ouch!
            CustomClass *shouldCrash = [[[CustomClass alloc] initWithoutArray] autorelease];
    
        }
        return 0;
    }
    

    【讨论】:

    • +1 用于回答问题并轻松证明我错了。 :)
    【解决方案2】:

    我不明白你为什么要这样做,但我建议你这样做:在你的子类中声明一个单独的 NSMutableArray 属性(我们称之为 nicestMutableArrayEver)并覆盖你的超类 NSArray 的 getter属性返回 mutableArray 实例:

    - (NSArray *)nicestArrayEver {
        return [self nicestMutableArrayEver];
    }
    

    这样,您可以在引用超类属性时取回 mutableArray。

    最好的,

    【讨论】:

    • 我不明白,为什么有人不想这样做。 CustomClass 和 ASubClassCustomClass 之间的这种关系是从常见到更特殊情况的一种特殊化——这是面向对象设计中非常典型的模式。另请注意,使用您的方法,将需要不断进行强制转换,同时关闭编译器警告。
    【解决方案3】:

    属性几乎不应该有可变类型。如果他们这样做了,那么调用者可以获取指针并在其背后改变对象的属性。如果一个属性应该是外部可变的,那应该是通过变异方法。

    请记住,属性定义接口,而不是实现。 @synthesize 可以从属性声明创建实现,但如果这样做会做错事,你不应该使用它。

    因此,第一步是为您的类和子类定义接口,而不考虑实现。只有在您知道接口应该是什么之后,您才应该设计每个接口的实现。

    无论属性是否在外部可变,支持的实例变量可能需要是可变的。类可能需要纯粹在内部改变自己的属性。

    您可以使基类具有指定的初始化程序,该初始化程序将数组对象作为参数(id 类型,这样子类的覆盖就不必强制转换以将其视为NSMutableArray*)。然后,该类的“普通”初始化程序将使用 NSArray 调用指定的初始化程序以供​​使用。子类的指定初始化器会调用超类的指定初始化器,传入NSMutableArray 来使用。

    或者,基类初始化程序可以调用另一个方法来获取数组。基类实现将返回一个NSArray。子类可以覆盖该方法以返回 NSMutableArray。

    【讨论】:

      【解决方案4】:

      你真的不能那样做。只需使用NSMutableArray 创建CustomClass。您可以将它们创建为 id 类型并检查 isKindOfClass: 但这只是一件苦差事,并不是真正必要的。

      我认为只有两个原因可以满足您的要求:

      1. 为了避免NSMutableArray 的额外开销
      2. 防止CustomClass 修改数组的内容,除非它是ASubClassCustomClass。

      虽然这些都是很好的目标,但我想说在这种情况下,稍微简化一下是值得的。

      【讨论】:

      • 乔希,请看我刚刚添加的注释。我同意你的看法,如果它是一个 NSArray,但在这种情况下,它不是,而且很重。
      • 无论如何,你还是在同一条船上。可能值得重新检查您的设计。如果孩子的信息类型与其超类不同,则可能值得将其存储在不同的变量中。如果你愿意,你可以创建一个类方法,根据你的子类返回适当的变量并覆盖它。
      • 实际上绝对有可能在子类中拥有同名但类型不同的属性。一个问题是(除非超类的 ivar 的可见性已更改)支持 ivar 必须具有不同的名称。请参阅 stackoverflow.com/a/10690692 或 vikingosegundo 对这个问题的回答。
      • @JacquesCousteau 是的,这对我来说是全新的。我确信情况并非如此,但显然我错了。我的立场是正确的。
      猜你喜欢
      • 2016-10-01
      • 2018-12-31
      • 1970-01-01
      • 1970-01-01
      • 2018-10-29
      • 2022-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多