【发布时间】:2018-11-10 23:42:20
【问题描述】:
我有一个关于python继承类方法的问题,在下面的代码中。
class B(object):
def test(self):
self.call()
def call(self):
print("Call from B")
if __name__ == "__main__":
b = B()
b.test()
from b import B
class C(B):
def call(self):
print("Call from C")
if __name__ == "__main__":
c = C()
c.test()
当我运行这段代码时,结果是
Call from C
父类方法会调用子类方法。我想知道这是否是预期和稳定的行为?因为我也在C++中尝试过同样的逻辑,所以会打印出来
Call from B
【问题讨论】:
-
你的问题是关于Python是否具有多态性?如果是这样the answer is yes it does
-
感谢您的回复。我只是想知道为什么 C++ 和 python 之间存在差异。
-
“为什么 C++ 和 python 之间存在差异”因为它们是 2 种不同的语言,而且功能完全不必完全相同。
标签: python inheritance