print('DerivedClass: self name equals ' + self.name)
if __name__ == '__main__':
class1 = BaseClass()
class1.getname()
class2 = DerivedClass()
class2.getname()
复制代码
运行结果:
BaseCalss: Constructor called
BaseCalss: self name equals BaseClass
BaseCalss: Constructor called
DerivedClass: Constructor called
self.name init value is BaseClass
DerivedClass: self name equals DerivedClass