【Python基础编程225 ● 面向对象 ● 多层继承 】


 ---------Python基础编程---------

Author : AI菌


【内容讲解】

多层继承:
1、一层一层的继承.
2、实际也就是单继承.

【代码演示】

"""
多层继承:
    1、一层一层的继承.
    2、实际也就是单继承.
"""


class A:
    def __init__(self, a):
        self.a = a

    def method_a(self):
        print("------method_a------")


class B(A):
    # 重写父类A中的__init__()方法
    def __init__(self, a, b):
        self.b = b
        # 调用父类A中被重写的__init__()方法
        super().__init__(a)

    def method_b(self):
        print("------method_b------")

    # 继承父类,相当于有method_a()方法
    # def method_a(self):
    #     print("------method_a------")


class C(B):
    # 重写父类C中的__init__()方法
    def __init__(self, a, b, c):
        self.c = c
        # 调用父类B中被重写的__init__()方法
        super().__init__(a, b)

    def method_c(self):
        print("------method_c------")

    # 继承父类,相当于有method_b()方法
    # def method_b(self):
    #     print("------method_b------")

    # 继承父类,相当于有method_a()方法
    # def method_a(self):
    #     print("------method_a------")


c1 = C("a的属性", "b的属性", "c的属性")
print(c1.a)
print(c1.b)
print(c1.c)

c1.method_c()
c1.method_b()
c1.method_a()

【往期精彩】

▷【Python基础编程196 ● 读取文件的4种方式】
▷【Python基础编程197 ● 读取文件的4种方式】
▷【Python基础编程198 ● 读取文件的4种方式】
▷【Python基础编程199 ● Python怎么读/写很大的文件】
▷【Python基础编程200 ● 读取文件的4种方式】
▷【Python基础编程201 ● 读取文件的4种方式】
▷【Python基础编程202 ● 读取文件的4种方式】
▷【Python基础编程203 ● 读取文件的4种方式】

【加群交流】



原文地址:https://www.cnblogs.com/hezhiyao/p/13425147.html