python继承

Posted acgame

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python继承相关的知识,希望对你有一定的参考价值。

继承的时候,调用父类的构造函数的方法:使用super()函数,只需要调用一次就行,会自动调用所有父类和父类的父类的构造函数

# -*- coding: utf-8 -*-
class A(object):
    def __init__(self):
        super(A, self).__init__()
        print("A__init__")

class B(object):
    def __init__(self):
        super(B, self).__init__()
        print("B__init__")

class C(A, B):
    def __init__(self):
        super(C, self).__init__() # 等价 super().__init__()
        print("C__init__")

x = C()

  

 输出为:

 

 

以上是关于python继承的主要内容,如果未能解决你的问题,请参考以下文章