Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中的多重继承和向继承类传递参数_Python_Python 2.7_Class_Inheritance_Multiple Inheritance - Fatal编程技术网

python中的多重继承和向继承类传递参数

python中的多重继承和向继承类传递参数,python,python-2.7,class,inheritance,multiple-inheritance,Python,Python 2.7,Class,Inheritance,Multiple Inheritance,我定义了以下类: class A(object): def __init__(self, a, b): self.a = a self.b = b self.c = "" def mA1(self): print "Method 1 in A" def mA2(self): print "Method 2 in A" def mA3(self): print "M

我定义了以下类:

class A(object):
    def __init__(self, a, b):
        self.a = a
        self.b = b
        self.c = ""

    def mA1(self):
        print "Method 1 in A"

    def mA2(self):
        print "Method 2 in A"

    def mA3(self):
        print "Method 3 in A"

class B(object):
    def __init__(self, a, b):
        self.a = a
        self.b = b
        self.x = 0

    def mB1(self):
        print "Method 1 in B"

class C(object):
    def __init__(self, a, b):
        self.a = a
        self.b = b
        self.c = ""
        self.y = 1

    def mC1(self):
        print "Method 1 in C"

    def mC2(self):
        print "Method 2 in C"
可以看出,它们在构造函数中采用相同的输入参数。现在,我想创建一个类
D
,它继承了
a
B
C
,这样我就可以直接将参数传递给
D
的构造函数,如下所示:

clsT = D(1, 2)
因此,我尝试了以下修订定义:

class A(object):
    def __init__(self, a, b):
        self.a = a
        self.b = b
        self.c = ""

    def mA1(self):
        print "Method 1 in A"

    def mA2(self):
        print "Method 2 in A"

    def mA3(self):
        print "Method 3 in A"


class B(A):
    def __init__(self, **kw):
        super(B, self).__init__(**kw)
        self.x = 0

    def mB1(self):
        print "Method 1 in B"


class C(A):
    def __init__(self, **kw):
        super(C, self).__init__(**kw)
        self.c = ""
        self.y = 1

    def mC1(self):
        print "Method 1 in C"

    def mC2(self):
        print "Method 2 in C"


class D(A, B, C):
    def __init__(self, a, b):
        super(D, self).__init__(a=a, b=b)


ci = D(1, 2)

print "a = ", ci.a
print "b = ", ci.b
print "c = ", ci.c
print "x = ", ci.x
print "y = ", ci.y
以上内容似乎不起作用,并给我以下错误:

    class D(A, B, C):
TypeError: Error when calling the metaclass bases
    Cannot create a consistent method resolution
order (MRO) for bases B, C, A

可能出了什么问题?是否真的要求
B
C
必须包含
super()
,在中级基类中创建线性继承?除了我的解释,还有别的选择吗?如果没有,如何调整我的类以使其工作?

我更改了
D
的类定义,如下所示,它工作了

class D(B, C, A):
    def __init__(self, a, b):
        super(D, self).__init__(a=a, b=b)
所以整个类看起来像,(我删除了方法以保持代码简短)

输出将是

1 2 0  1
这是由于算法中的一条规则,它说(更多细节在这里,但要点是

类总是出现在其祖先之前(“单调性”)

因此B和C需要出现在A之前,因为A是B和C的祖先

换句话说:
D是从A、B和C继承的。因为B和C已经从A继承了,所以python现在无法确定首先查找方法的类是什么;如果使用旧的定义顺序,可以是A,也可以是B和C。
D(A,B,C)

太好了!那么,错误实际上总是指向实际的顺序?是的,错误说,这一切都不能创建一致的方法解析,一致性问题是因为我在回答@skrowten\u hermit时指出的
1 2 0  1