Mixin覆盖继承的方法

时间:2016-02-22 18:41:54

标签: python inheritance multiple-inheritance mixins

我有一系列类,A1,A2,A3等,它们都有方法m()。我也有方法m()的B类。我希望能够轻松地创建从类B调用m()的类C1,C2,C3等,同时还具有A1,A2,A3等所有其他属性...

然而,我遇到的问题是,在C1类中,B类的方法m()应该从A1类调用m()

我很难说出我想要的内容,但我目前正在考虑这样做的方式是使用mixins。 C1将继承自A1,混合使用B.但是,我不知道如何让B中的m()从其中一个A类调用正确的m()

所以,我的两个问题:

  • 我试图做的是什么名字?
  • 这样做的正确方法是什么?

编辑:根据要求,一个具体的例子: 对于某些参数m(p),A1,A2,A3等中的方法M都计算矩阵p。我想创建类C1,C2,C3等,其行为方式与方法m()的A1,A2,A3,相同。新方法m()需要较长的参数列表p,大小为N,我们计算A*.m() N次,然后返回总和。

计算m()之和的代码对于所有A *类都是相同的。在上面提出的混合解决方案中,求和代码将在B中.B和A1都将被继承以形成C1。但是,来自B的m()中的方法C1必须调用A1.m()

1 个答案:

答案 0 :(得分:2)

我认为你只需要super将调用重定向到父类或兄弟类(取决于MRO)。

例如:

class A1(object):
    def m(self):
        print('Calling method m of class A1')
        self.data *= 2

class A2(object):
    def m(self):
        print('Calling method m of class A2')
        self.data *= 3

class A3(object):
    def m(self):
        print('Calling method m of class A3')
        self.data *= 4

class B(object):
    def m(self, p):
        print('Calling method m of class B')
        for i in range(p):
            # You haven't specified which python you are using so I assume
            # you might need to most explicit variant of super().
            # Python3 also allows just using super().m()
            super(B, self).m()

class C1(B, A1):
    def __init__(self, value):
        self.data = value

只是测试一下:

a = C1(10)
a.m(10)

打印:

Calling method m of class B
Calling method m of class A1
Calling method m of class A1
Calling method m of class A1
Calling method m of class A1
Calling method m of class A1
Calling method m of class A1
Calling method m of class A1
Calling method m of class A1
Calling method m of class A1
Calling method m of class A1

和保存的值:

a.data
# returns 10485760

定义其他C也有效:

class C2(B, A2):
    def __init__(self, value):
        self.data = value

a = C2(10).m(2)
#Calling method m of class B
#Calling method m of class A2
#Calling method m of class A2


class C3(B, A3):
    def __init__(self, value):
        self.data = value

a = C3(10).m(1)
#Calling method m of class B
#Calling method m of class A3

当然你想要另一个逻辑,可能需要从.m()返回值,而不是就地修改,但我认为你可以自己解决。

你要找的词可能是MRO (method resolution order)。希望这会对你有所帮助。

同样令人感兴趣的可能是super (Python2)super (Python3)的文档。

您可以随时通过调用MRO方法检查课程的.mro()

print(C1.mro())
[<class '__main__.C1'>, <class '__main__.B'>, <class '__main__.A1'>, <class 'object'>]

所以python首先检查C1是否有方法m,如果不检查BB有一个,所以它被执行。然后,super调用再次进入MRO并检查下一个类(A1)是否有方法m,然后执行该方法。

相关问题