什么是无操作类?
我刚刚在这个答案中了解了模板方法模式< /a> 关于确保子类始终调用基类中的方法的问题。
部分答案讨论了基类与无操作类:
(决定是否做 无操作或抽象通常是相当好的 显而易见 - 基类是否使 作为一个具体的事物,它本身就具有意义 类?)
什么是无操作类?
I just learned about the Template Method pattern in this answer to a question about ensuring that methods in base classes are always called by child classes.
Part of the answer talks about base classes vs no-op classes:
(The decision about whether to make it
a no-op or abstract is usually fairly
obvious - does the base class make
sense on its own, as a concrete
class?)
What is a no-op class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有操作的不是类,而是方法的实现。
答案争论的是使基类中的方法抽象(从而强制派生类实现它,即使它们完全不希望它执行任何操作)还是将其实现为“无操作” “(即方法体为空)。
如果什么都不做是一个合理的默认值,那么基类可能会将此方法实现为无操作,以便派生类仅在它们实际上希望发生不同的事情时才费心重写。
如果什么都不做是不明智的,那么方法是抽象的就有意义了。
It isn't the class that is no-op, but the implementation of the method.
The answer debates the decision of making the method in the base class abstract (thereby forcing derived classes to implement it, even if they want to have it do absolutely nothing) versus implementing it as a "no-op" (i.e. with an empty method body).
If doing nothing is a sensible default, then the base class might implement this method as a no-op so that derived classes are bothered to override only if they actually want something different to happen.
If doing nothing is not sensible, then it makes sense for the method to be abstract.
no-op
是no-operation
的缩写,即不执行任何操作的方法:或者来自链接的问题:
no-op
is short forno-operation
, that is, a method that does nothing:Or from the linked question: