20日目:アプリケーションを継承し、スーパーアプリケーションを派生させ、ミックスインメカニズムの組み合わせを見つけます

アプリケーションテンプレートの継承

学生选课系统案例模板
class Student:
    school = "虹桥校区"

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

    def choose(self):
        print("%s 选课成功" % self.name)


stu1 = Student("lili", 18, "female")
stu2 = Student("nana", 16, "female")
stu3 = Student("dada", 19, "male")


class Teacher:
    school = "虹桥校区"

    def __init__(self, name, age, gender, level):
        self.name = name
        self.age = age
        self.gender = gender
        self.level = level

    def score(self):
        print("%s 正在为学生打分" % self.name)


tea1 = Teacher("egon", 18, "male", 10)
tea2 = Teacher("lxx", 26, "male", 3)

派生
サブクラス派生の新しいメソッドで
親クラスの関数再利用します。親クラスの関数を継承した、サブクラスは新しい関数を定義します。この形式は派生と呼ばれます。

方式一:指名道姓地引用某一类的函数,与继承无关
class Abb:
    school = "虹桥校区"

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


class Student(Abb):
    def choose(self):
        print("%s 选课成功" % self.name)


stu1 = Student("lili", 18, "female")
stu2 = Student("nana", 16, "female")
stu3 = Student("dada", 19, "male")


# 继承了父类的功能后,子类定义了新的功能这种形式称为派生
class Teacher(Abb):
    def __init__(self, name, age, gender, level):  # 形参
        Abb.__init__(self, name, age, gender)  # 实参
        self.level = level

    def score(self):
        print("%s 正在为学生打分" % self.name)


tea1 = Teacher("egon", 18, "female", 10)
tea2 = Teacher("biu", 19, "male", 20)
print(tea1.__dict__)

スーパーアプリケーション

方式二:super()返回一个特殊的对象,该对象会参考发起属性查找的那一个类的mro列表.去当前类的父类中找属性
class Abb:
    school = "虹桥校区"

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


class Student(Abb):
    def choose(self):
        print("%s 选课成功" % self.name)


stu1 = Student("lili", 18, "female")
stu2 = Student("nana", 16, "female")
stu3 = Student("dada", 19, "male")


# 继承了父类的功能后,子类定义了新的功能这种形式称为派生
class Teacher(Abb):
    def __init__(self, name, age, gender, level):  # 形参
        # Abb.__init__(self, name, age, gender)  # 实参
        super().__init__(name, age, gender)  # super()返回一个特殊的对象,该对象会参考自己当前所在的那一个类的mro列表.去当前类的父类中找属性
        # super(Teacher,self).__init__(name, age, gender) python2中的用法
        self.level = level

    def score(self):
        print("%s 正在为学生打分" % self.name)


tea1 = Teacher("egon", 18, "female", 10)
tea2 = Teacher("biu", 19, "male", 20)
print(tea1.__dict__)

継承属性検索
単一継承の検索順序、クラシッククラスと新しいクラスの検索順序は同じです
mro()はクラスの継承順序を表示し、morリストは左から右に親クラスを検索しますこれに一致する最初のものが見つかるまで。属性クラス

class C:
    x = 222


class B(C):
    pass


class A(B):
    pass


obj = A()
print(obj.x)
print(A.mro())  # [<class '__main__.A'>, <class '__main__.B'>, <class '__main__.C'>, <class 'object'>]
print(B.mro())  # [<class '__main__.B'>, <class '__main__.C'>, <class 'object'>]

多重継承のコンテキストでは
、新しいスタイルのクラスとクラシッククラスが最後に同じポイントに収束しない場合、検索順序は同じであり、
新しいスタイルのクラスは最初にブランチの深さから検索されます。クラシッククラスには多重継承背景ダイヤモンド継承があります。検索順序の違い

ダイヤモンド継承/ダイヤモンド継承:サブクラスによって継承され、最終的に非オブジェクトクラスに収束する複数のブランチ
クラシッククラス:深さ優先、
新しいスタイルクラス:幅優先

クラシック
ここに画像の説明を挿入

新しいスタイル
ここに画像の説明を挿入

新式类菱形继承查找代码呈现
class G(object):
    def test(self):
        print('from G')


class E(G):
    def test(self):
        print('from E')


class F(G):
    def test(self):
        print('from F')


class B(E):
    def test(self):
        print('from B')


class C(F):
    def test(self):
        print('from C')


class D(G):
    def test(self):
        print('from D')


class A(B, C, D):
    def test(self):
        print('from A')

    pass


obj = A()
obj.test()  # 查找顺序为:obj->A->B->E->C->F->D->G->object

場合:

class A:
    def test(self):
        print("from A")
        super().test()


class B:
    def test(self):
        print("from B")


class C(A, B):  # 查找顺序[C,A,B,object]
    pass


obj = C()
obj.test()
from A
from B
obj = A()  # 查找顺序[A,object]
obj.test()
from A ,object 里面没有test()所以会报错
注意:新式类里面的查找顺序,以属性发起者优先,找不到会从发起者的父类里面去找属性,
如果父类里面没有,会从第二个父类里面查找,应当遵循广度优先的原则

ミックスインメカニズム(多重継承命名規則)
継承は、is-a関係を表します(可能な限り、サブクラスを親クラスに属し、多くの反復コードを親クラスに配置し、親クラスをに配置します)右端、他の親クラスは主に関数を追加するために使用されます)

代码呈现
class Vehicle:
    pass


class FlyableMixin:
    def fly(self):
        print("flying")


class CivilAircraft(FlyableMixin, Vehicle):
    pass


class Helicopter(FlyableMixin, Vehicle):
    pass


class Car(Vehicle):
    pass

組み合わせ

組み合わせ:組み合わせは、has-a関係を表します。オブジェクトのプロパティは、値が別のクラスのオブジェクトを指すことです。

class Abb:
    school = "虹桥校区"

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


class Student(Abb):
    def choose(self):
        print("%s 选课成功" % self.name)


# 继承了父类的功能后,子类定义了新的功能这种形式称为派生
class Teacher(Abb):
    def __init__(self, name, age, gender, level):  # 形参
        Abb.__init__(self, name, age, gender)  # 实参
        self.level = level

    def score(self):
        print("%s 正在为学生打分" % self.name)


class Course:
    def __init__(self, name, price, period):
        self.name = name
        self.price = price
        self.period = period

    def tell(self):
        print("课程信息:%s %s %s" % (self.name, self.price, self.period))


python = Course("python", 20000, "6mons")
linux = Course("linux", 20000, "5mons")

stu1 = Student("lili", 18, "female")
stu2 = Student("nana", 16, "female")
stu3 = Student("dada", 19, "male")

tea1 = Teacher("egon", 18, "female", 10)
tea2 = Teacher("biu", 19, "male", 20)

# stu1.course = python
# stu1.course.tell()

stu1.courses = []
stu1.courses.append(python)
stu1.courses.append(linux)

print(stu1.courses)
for course_obj in stu1.courses:
    course_obj.tell()

おすすめ

転載: blog.csdn.net/Yosigo_/article/details/112546626