1. 程式人生 > >面向對象中,用super來聯系父類的函數

面向對象中,用super來聯系父類的函數

col super 面向 elf 結果 執行 mon other 對象

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‘}

面向對象中,用super來聯系父類的函數