面向对象中,用super来联系父类的函数

class Animal:
    def __init__(self):
        print("A构造方法")
        self.ty = "动物"
    def Other(self):
        print("Others")
        self.nn = "simon"


class Cat(Animal):

    def __init__(self):
        print("B构造方法")
        self.n = ""
        # super执行父类的构造方法,可以执行父类的任何方法
        super(Cat, self).__init__()
        super(Cat,self).Other()


c = Cat()
print(c.__dict__)

执行结果:

B构造方法
A构造方法
Others
{'n': '猫', 'ty': '动物', 'nn': 'simon'}

posted @ 2018-10-13 17:14  xuwenwei  阅读(88)  评论(0编辑  收藏  举报