我目前对某些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
双下划线是否有效使方法成为私有,因此不能被子类覆盖?