为什么子类不能覆盖'私有' Python中的基本方法?

时间:2017-06-09 07:53:17

标签: python inheritance virtual-method

我目前对某些Python行为感到震惊:

class Base(object):
    def foo(self):
        self.__virtual_function()
        self.virtual_function()
    def __virtual_function(self):
        print("private Base")
    def virtual_function(self):
        print("public Base")

class Derived(Base):
    def __virtual_function(self):
        print("private Derived")
    def virtual_function(self):
        print("public Derived")

d = Derived()
d.foo()

打印

private Base
public Derived

双下划线是否有效使方法成为私有,因此不能被子类覆盖?

0 个答案:

没有答案