开发者

How does one know when it's safe to use a parent method in NS subclasses?

As an example, when I'm using an NSMutableDictionary, I know it inherits all the methods of NSDictionary, but how can I know/trust that it has overridden the behavior of those methods if I want to use NSDictionary methods (such as +dictionaryWithObjectsAndKeys) to create my mutable dictionary instance?

More generally, is it the Framework's responsibility to make sure subclasses don't blindly inherit methods that can potentially break instances of the subclass if used? Or is it the coder's responsibility to know not to use them? If Square inherits from Rectangle, and via inheritance I can call

Square *newSquare = [[Square alloc] init];
[newSquare setWidth:3 andHeight:6]; //instead of -(void)setSide:(int)side

I've "broken" the square and other methods which dep开发者_如何学Cend on width being equal to height will not work now. What are the rules of the game?


The rule would be only expose what you would allow to be override it means, put on your interface what is really public. When necessary explicitly state that when overriding an specific method call at some point [super methodName].

On your example you would override the method - (void)setWidth:(int)width andHeight:(int)height, and you would like to throw an error if width != height. Or you could also throw an error and force the user to only use - (void)setSide:(int)side.

For example you could do:

// If you want to test and accept cases when width == height
- (void)setWidth:(int)width andHeight:(int)height {
    NSAssert(width == height, NSLocalizedString(@"This is a Square. Width has to be height.", nil));

    [super setWidth:width andHeight:height];

    // Or

    [self setSide:width];
}

// Or if you want to completely prohibit the usage of the method
- (void)setWidth:(int)width andHeight:(int)height {
    NSAssert(NO, NSLocalizedString(@"This is a Square! Please use - (void)setSide:(int)side method.", nil));
}

If you would like to throw some errors and warnings at compilation time, you could use on the declaration of your methods, some of the macros defined on NSObjCRuntime.h.


I wouldn't trust the parent convenience method to call your inheriting init method. For example, that dictionary method could be defined as:

+ (id)dictionaryWithObjectsAndKeys:...
{
    return [[[NSDictionary alloc] initWithObjectsAndKeys:...] autorelease];
}

If that method is defined that way then it won't be even be aware of your implementation.

You'd have to create your own convenience method. Something like would be in your MyDictionary implementation:

+ (id)myDictionaryWithObjectsAndKeys:...
{
    return [[[MyDictionary alloc] initWithObjectsAndKeys:...] autorelease];
}

--

Also...

You probably should inherit Rectangle from Square. Inheritance is additive. You can describe Square with one size (width), but for Rectangle you have two sizes (width, height).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜