当父类的方法不能满足子类的需求的时候,可以对父类的方法进行重写
1.覆盖父类方法
2.对父类方法进行扩展
覆盖:
class Animal:
def eat(self):
print('吃')
def drink(self):
print('喝')
def run(self):
print('跑')
def sleep(self):
print('睡')
class Cat(Animal):
def call(self):
print('miaomiao')
class Hello_Kitty(Cat):
def speak(self):
print('我可以说日语')
def call(self):
print('sakrduqy')
输出覆盖了父类call的方法
扩展父类的方法:
我们希望hellokitty不仅会父类中miaomiao还会sakrduqy,所以用super().xx()的方法
class Animal:
def eat(self):
print('吃')
def drink(self):
print('喝')
def run(self):
print('跑')
def sleep(self):
print('睡')
class Cat(Animal):
def call(self):
print('miaomiao')
class Hello_Kitty(Cat):
def speak(self):
print('我可以说日语')
def call(self):
# Cat.call(self)
super().call()
print('sakrduqy')
hk = Hello_Kitty()
hk.speak()
hk.call()