这是您要求的示例。打印chocolate
。
class Base:
def foo(self):
print("foo")
def bar(self):
self.foo()
class Derived(Base):
def foo(self):
print("chocolate")
d = Derived()
d.bar() # prints "chocolate"
该字符串chocolate
被打印,而不是foo
因为Derived
覆盖该foo()
函数。即使在bar()
中进行了定义Base
,它最终还是调用的Derived
实现foo()
而不是Base
实现。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句