【问题标题】:Mixin to override inherited methodMixin 覆盖继承的方法
【发布时间】:2016-06-04 08:01:12
【问题描述】:

我有一个类的集合,A1、A2、A3 等,它们都有方法m()。我也有 B 类,方法为m()。我希望能够轻松地创建从 B 类调用 m() 的类 C1、C2、C3 等,同时还具有 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 等,它们的行为方式与方法 m() 的 A1、A2、A3、except 相同。新方法m() 需要更长的参数列表p,大小为N,我们计算A*.m() N 次,然后返回总和。

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

【问题讨论】:

  • 你能举几个例子吗?

标签: python inheritance multiple-inheritance mixins


【解决方案1】:

我认为您只需要 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,然后执行该方法。

【讨论】:

  • 您可能想澄清super() 并不总是调用父级,它会在MRO 中的下一个类 中查找请求的属性。在这种情况下,它可以找到一个兄弟类。
猜你喜欢
  • 2012-11-20
  • 2016-01-25
  • 1970-01-01
  • 2011-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-27
  • 1970-01-01
相关资源
最近更新 更多