检查布尔值后在子类中重写
我计划如果将布尔值设置为某个值,则重写子类中的方法,然后切换该值,并想到了两个选项:
1。调用super并检查是否成功
// Superclass
-(BOOL) showEngine
BOOL result = !self.isEngineActive;
if (result) {
// Does something common to both super and subclass
self.isEngineActive = YES;
}
return result;
}
// Subclass override
-(BOOL) showEngine
BOOL result = [super showEngine];
if (result) {
// Does something unique to subclass
}
return result;
}
2.委托给第二个方法
// Superclass
-(void) showEngine
if (!self.isEngineActive) {
// Delegate
showEngineNow();
// Does something common to both super and subclass
self.isEngineActive = YES;
}
}
// Subclass override
-(void) showEngineNow() {
[super showEngineNow];
// Does something unique to subclass
}
第一个方法具有必须调用 super 并检查结果的负面影响,而第二个方法则具有对委托调用第二个方法的负面影响。哪一种方法更好,还有我没有想到的吗?
I plan to override a method in the subclass if a boolean is set to a certain value, and then to switch this value, and have thought of two options:
1. Calling super and checking success
// Superclass
-(BOOL) showEngine
BOOL result = !self.isEngineActive;
if (result) {
// Does something common to both super and subclass
self.isEngineActive = YES;
}
return result;
}
// Subclass override
-(BOOL) showEngine
BOOL result = [super showEngine];
if (result) {
// Does something unique to subclass
}
return result;
}
2. Delegate to a second method
// Superclass
-(void) showEngine
if (!self.isEngineActive) {
// Delegate
showEngineNow();
// Does something common to both super and subclass
self.isEngineActive = YES;
}
}
// Subclass override
-(void) showEngineNow() {
[super showEngineNow];
// Does something unique to subclass
}
The first method has the negative effect of having to call super and check the result, whereas the second has the negative baggage of a second method call to the delegate. Which would be the better way, and is there one I haven't thought of?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在重写方法时调用 super 实现是很常见的。我不认为这是负面的。从这个问题中尚不清楚你想要实现什么目标,但我会选择选项 1 - 为什么要引入更多的复杂性?
这是假设您在超级实现中执行的未指定的
//stuff
也应该由子类完成。如果没有,您将需要分成单独的方法。我认为您没有使用 Cocoa 中的“委托”。如果它只是同一个类中的另一个方法,那么它就不是委托。委托是符合已知协议的单独对象。
如果您觉得这些方法太长并且做了太多事情,那么请务必将它们分解并仅覆盖子类中的相关部分。在这种情况下,选项 2 是您最好的选择,但不要将其称为委托。
Calling the super implementation is pretty common when overriding a method. I don't see this as a negative. It's not clear from the question what you are trying to acheive, but I'd go with option 1 - why introduce more complexity?
This is assuming that the unspecified
//stuff
you are doing in the super implementation should also be done by the subclass. If not, you will need to split out into separate methods.I don't think you are using "delegate" as it is intended in Cocoa. If it's just another method in the same class, it isn't a delegate. A delegate is a separate object that conforms to a known protocol.
If you feel that the methods are getting too long and doing too many things, then by all means break them up and override only the relevant parts in your subclass. In that case option 2 is your best bet, but don't call it a delegate.