Mixin覆盖继承的方法

问题描述 投票:1回答:1

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

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

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

那么,我的两个问题:

  • 我正在尝试做什么名称?
  • 这样做的正确方法是什么?

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

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

python inheritance multiple-inheritance mixins
1个回答
3
投票

我想你只需要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,然后执行。

© www.soinside.com 2019 - 2024. All rights reserved.