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 写生日祝福语简短唯美
15 2026猪年祝福语简短语
16 孙媳妇祝福语简短
17 对睡觉的祝福语简短
18 团体年会祝福语简短霸气