python——多重继承

2023-01-31 01:01:26 python 继承
#方式一
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()


相关文章