super()的使用

class Cat:
    """
    这是一只猫的类
    """
    def __init__(self,name,height):
        self.name = name,
        self.height = height
        # print(f"我是一只猫,我叫{self.name}")

    def eat(self):
        print("我爱吃鱼")


class Dog(Cat):

    def __init__(self,name):
        self.name = name
        print("_____",name)

    def eat(self):
        super().__init__(name=self.name, height=10)
        print(self.height)
        super().eat()
        print("我爱吃骨头")

#继承时,子类有属性,怎么调用父类的属性

class A(object):  #object顶级父类

    #定义实力属性name
    def __init__(self):
        self.name = '我是A类里面的name属性'

    def run(self):
        print("A跑")

class B(A):
    def __init__(self):
        super().__init__()
b = B()
print(b.name)

#子类继承父类,并且子类和父类有相同的方法,子类如何调用父类的方法

class Animal:
    def heshui(self):
        print('动物正在喝水')

class Cat(Animal):
    def heshui(self):
        super().heshui()
cat = Cat()
cat.heshui()


版权声明:本文为lizhenqi123456原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。