如果父函数没有这样的方法,父函数调用是否应该调用父函数的父函数?
出于学术目的,我想知道如果父级没有这样的方法,父级方法调用是否应该导致父级的父级方法调用?
例如(伪代码):
class A {
function doSomething() {
}
}
class B extends A {}
class C extends B {
function doSomething() {
parent::doSomething();
}
}
i = new C();
i->doSomething();
面向对象语言中的父级调用是否意味着如果父级缺少该方法,则其父级将调用其方法?
For academic purposes, I am wondering if a parent method call should lead to a parent's parent method call in case when the parent has no such method?
For example (pseudo-code):
class A {
function doSomething() {
}
}
class B extends A {}
class C extends B {
function doSomething() {
parent::doSomething();
}
}
i = new C();
i->doSomething();
Does parent calling in object oriented languages mean that if the parent is missing the method, its parent will have its method called instead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 OOP 中,这个问题通常通过函数
doSomething
的可见性来回答。如果它在类
A
中是受保护的或公共的,那么是,如果它没有在B
中被重写(我假设这里也
extends
关键字是公共继承,请参阅http://www.learncpp.com/cpp-tutorial/115-inheritance-and-access-specifiers/ 了解更多信息。)In OOP this question is usually answered by the function
doSomething
's visibility.If it was protected or public in class
A
, then yes, it will be called if it is not overridden inB
(I assume here also that the
extends
keyword is public inheritance. See http://www.learncpp.com/cpp-tutorial/115-inheritance-and-access-specifiers/ for more information.)父子“扩展”/单继承的语义强烈暗示“只要可以使用父级,您也可以使用子级”。
也就是说,孩子的特征应该是父母的特征的超集。
因此,由于上述原则,大多数理智的单继承实现都会使子级的默认行为与父级相同。
当然,可能还有其他因素在起作用,例如语言可能支持接口、按契约设计等——在这种情况下,父方法可能是一个存根或抽象的不可调用方法,或者谁知道什么策略可能会生效。
注意:其他答案关于公共与私有的讨论实际上是特定于类 Java 语言的,而不是(正如问题标签所暗示的那样)一般的 OOP。除非您认为可见性是一般 OOP 的一部分(Python 语言可能会反对)。
Semantics of parent-child "extends"/single inheritance strongly imply that "wherever you could use the parent, you may also use the child".
That is, the features of the child should be a superset of the parent's.
Thus, most sane implementations of single-inheritance will make the default behavior of the child identical to the parent, because of the above principle.
Of course there may be other things at work, for example the language may support interfaces, design-by-contract, etc. -- in which case the parent method may be a stub or abstract uncallable method, or who-knows-what policy may be in effect.
note: The other answers's talk about public-vs-private are really specific to Java-like languages, and not (as the question's tags imply) general OOP. Unless you consider visibility to be part of general OOP (the python language might object).
如果 A 上定义的 DoSomething 的范围是 Public,那么 DoSomething 也是 B 的 Public 接口的一部分。因此,DoSomething方法也将成为C公共接口的一部分。由于 A 上定义的原始方法由派生类继承,因此除非重写父方法,否则派生类将调用父方法。
在某种程度上,所有在特定范围内如何工作的细节可能由特定语言决定,但一般的面向对象原则遵循上述概念。 。 。
If the scope of DoSomething as defined on A is Public, then DoSomething is also a part of B's Public intefrace as well. Therefore, the DoSomething method will also be a part of C's public interface. Since the original method as defined on A in inherited by derived classes, the parent method will be called by derived classes unless it is overridden.
TO a degree, the details of how all that works within certain scopes might be deteermined by specific languages, but general OO principles follow the concept above . . .