重载

所谓重载,就是子类中,有一个和父类相同名字的方法,在子类中的方法会覆盖掉父类中同名的方法

#coding=utf-8
class Cat:
    def sayHello(self):
        print("halou-----1")


class Bosi(Cat):
    def sayHello(self):
        print("halou-----2")

bosi = Bosi()
bosi.sayHello()

 运行结果:halou-----2

2. 调用父类的方法

#coding=utf-8
class Cat:
    def __init__(self,name):
        self.name = name
        self.color = 'yellow'


class Bosi(Cat):

    def __init__(self,name):
        Cat.__init__(self,name) # 调用父类的__init__方法

    def getName(self):
        return self.name

bosi = Bosi('xiaohua')

print(bosi.name)
print(bosi.color)

 运行结果:xiaohua

       yellow

原文地址:https://www.cnblogs.com/loaderman/p/6561825.html