当子类和父类有相同的方法的时候,子类默认会调用自己的方法而不能使用父类的方法。
如果想使用父类的方法,我们就可以使用super()方法来调用父类的方法
1.super(类名,对象名).方法() ------------>既可以在类的内部也可以在类的外部使用。
2.父类类名.方法名(self)------------------->Animal.eat(self) 既可以在内部也可也在外部使用
3.super().eat()---------------> 只能在类的内部。(推荐)
"""
当子类和父类有相同的方法的时候,子类默认会调用自己的方法
而不能使用父类的方法。
如果想使用父类的方法,我们就可以使用super()方法来调用父类的
方法
1.super(类名,对象名).方法()既可以在类的内部也可以在类的外部使用。
2.父类类名.方法名(self)-->Animal.eat(self) 既可以在内部也可也在外部使用
3.super().eat()--> 只能在类的内部。
"""
class Animal(object):
type = '动物'
def eat(self):
print('吃...')
def sleep(self):
print('睡觉')
class Dog(Animal):
def look_house(self):
print('看大门...')
def eat(self):
# super().eat() # 调用的是父类的eat()方法。
# Animal.eat(self) # 父类类名.方法名(self)
print('吃狗粮...')
dog = Dog()
# Animal.eat(dog)
super(Dog, dog).eat() # super(类名,对象名).方法()
dog.eat()