超级'怎么样?在Python中使用带有参数的多继承?

时间:2017-08-18 19:50:34

标签: python python-2.7 multiple-inheritance super

我有以下代码:

class TestA(object):
    def __init__(self, *args):
        super(TestA, self).__init__()
        print 'TestA'

class TestB(object):
    def __init__(self, my_var=None):
        super(TestB, self).__init__()
        print 'TestB', my_var

class TestC(object):
    def __init__(self, my_var=None):
        super(TestC, self).__init__()
        print 'TestC', my_var

class TestD(TestB, TestA, TestC):
    def __init__(self):
        print 'TestD'
        super(TestD, self).__init__("Hello World")

TestD() # Start from here

输出

TestD
TestC None
TestA None
TestB Hello World

我希望有:

TestD
TestB Hello World
TestA Hello World
TestC Hello World

我的代码或理解有什么不对?

如果我在 TestA TestB TestC 上删除超级,我会拨打3次超级如下所示,我有预期的输出。 (但我不想打电话三次超级)

class TestD(TestB, TestA, TestC):
    def __init__(self):
        print 'TestD'
        super(TestD, self).__init__("Hello World")
        super(TestD, self).__init__("Hello World")
        super(TestD, self).__init__("Hello World")

提前谢谢。

0 个答案:

没有答案