Python继承

super()

class People:

    def __init__(self, name, sex, age):
        self.name = name
        self.sex = sex
        self.age = age

class Student(People):

    def __init__(self, name, sex, age, score):
        super().__init__(name, sex, age)
        self.score = score

class Teacher(People):

    def __init__(self, name, sex, age, course):
        People.__init__(self, name, sex, age)
        self.course = course

xiaoming = Student("小明", "男", 8, 100)
yanpeng = Teacher("徐筵彭", "男", 25, "编程")

print(xiaoming.name, xiaoming.sex, xiaoming.age, xiaoming.score)
print(yanpeng.name, yanpeng.sex, yanpeng.age, yanpeng.course)

继承顺序

class A:
    pass

class B(A):
    pass

class C(A):
    pass

class D(B):
    pass

class E(C):
    pass

class F(D):
    pass

class G(F, E):
    pass

print(F.mro())
print(G.mro())
[, , , , ]
[, , , , , , , ]

你可能感兴趣的:(Python继承)