Python中的重写方法
您始终可以覆盖父类方法。覆盖父方法的原因之一是因为您可能需要子类中的特殊功能或其他功能。
示例
#!/usr/bin/python
class Parent: # define parent class
def myMethod(self):
print 'Calling parent method'
class Child(Parent): # define child class
def myMethod(self):
print 'Calling child method'
c = Child() # instance of child
c.myMethod() # child calls overridden method输出结果
执行以上代码后,将产生以下结果-
Calling child method
热门推荐
10 道教生日祝福语简短大全
11 表妹结婚祝福语简短藏语
12 简短祝福语蛋糕简笔画
13 给舍友送祝福语简短
14 工厂祝福语简短10字
15 牛年新年祝福语大全简短
16 18岁的祝福语简短
17 敏字的祝福语简短
18 宝宝祝福语诗意简短 古文