python——多重继承

发布时间:2019-07-29 10:29:05编辑:auto阅读(1834)

    #方式一
    class A():
        def __init__(self, name):
            self.name = name
            print("a class ...")
    
    class B():
        def __init__(self, age):
            self.age = age
            print("b class...")
    
    class Sub(A, B):
        def __init__(self, name, age, phone):
            A.__init__(self, name)
            B.__init__(self, age)
            self.phone = phone
            print("sub class...")
    
        def get_all_info(self):
            print(self.name, self.age, self.phone)
    
    if __name__ == '__main__':
    
        s = Sub("toby", 25, 110)
        s.get_all_info()
    
    
    #方式二
    class A():
        def __init__(self, name):
            self.name = name
            print("a class ...")
    
    class B(A):
        def __init__(self, name, age):
            super(B, self).__init__(name)
            self.age = age
            print("b class...")
    
    class Sub(B):
        def __init__(self, name, age, phone):
            super(Sub, self).__init__(name, age)
            self.phone = phone
            print("sub class...")
    
        def get_all_info(self):
            print(self.name, self.age, self.phone)
    
    if __name__ == '__main__':
    
        s = Sub("toby", 25, 110)
        s.get_all_info()


关键字