简体   繁体   English

多类继承

[英]Multi-class inheritance

I'm studying about OOP method.我正在研究 OOP 方法。 but now I'm stuck with this.但现在我坚持这个。 The model I want is class X and class Y. it has many calculations and different method.我想要的模型是 X 类和 Y 类。它有很多计算和不同的方法。 The class X and Y need input to in the class.类 X 和 Y 需要在类中输入。 Then bring the output from class X.cal1 and Y.cal2.然后带来类 X.cal1 和 Y.cal2 的输出。 To summarize in class Z. but class Z need input for calculation in class themselves.在Z 类中进行总结。但Z 类需要在类中自己进行计算的输入。 The code is an example to represent my calculation flow.该代码是代表我的计算流程的示例。 Not an actual my code.不是实际的我的代码。 it looks like every class need input.看起来每个班级都需要输入。 But it can't make one big class, then have many functions in the class.但它不能成为一个大类,然后在类中有很多功能。 it needs to be class themselves.它需要自己上课。 Then join all class in the new class.然后加入新班级的所有班级。 Now my code doesn't work.现在我的代码不起作用。

class X():
    def __init__(self,x):
        super(X, self).__init__()
        self.x = x

    def cal1(self):
        return self.x*2

class Y():
    def __init__(self,y):
        super(Y, self).__init__()
        self.y = y

    def cal2(self):
        return self.y/2

class Z(X,Y):
    def __init__(self,Z):
        super(Z, self).__init__()
        self.Z = Z
        self.xx = X.cal1()
        self.yy = Y.cal2()

    def cal3(self):
        return self.z+self.xx+self.yy

if__name__=='__main__':
   R1 = X(30)
   R2 = Y(20)
   R3 = Z(R1,R2,50)

I don't like the approach, but you can use:我不喜欢这种方法,但您可以使用:

class Z(X, Y):
    def __init__(self, x, y, z):
        super().__init__(x)
        super(X, self).__init__(y)
        self.z = z

And remove the super() calls in X and Y .并删除XY中的super()调用。

Another approach is using kwargs or not having arguments in __init__ so that you can keep the same init signature in all classes.另一种方法是使用 kwargs 或在__init__不使用参数,以便您可以在所有类中保持相同的 init 签名。 Once you do that, you may use super() and the Method Resolution Order (MRO) to call the init functions.完成此操作后,您可以使用super()和方法解析顺序 (MRO) 来调用 init 函数。 The call stack would look like this:调用堆栈如下所示:

  • Z(x=.., y=.., z=..) calls Z.__init__ Z(x=.., y=.., z=..)调用Z.__init__
  • Z.__init__ gets the z kwarg and calls super().__init__(**kwargs) which is X.__init__ Z.__init__获取z kwarg 并调用super().__init__(**kwargs)X.__init__
  • X.__init__ gets the x kwarg and calls super().__init__(**kwargs) which is Y.__init__ X.__init__获取x kwarg 并调用super().__init__(**kwargs)Y.__init__
  • Y.__init__ gets the y kwarg and calls super().__init__() which is object.__init__ Y.__init__获取y kwarg 并调用super().__init__()这是object.__init__

Note: super() without arguments only works in Python 3.注意:不带参数的super()仅适用于 Python 3。

The last approach I can think of is not (!) using super and just calling X.__init__(self, x) and Y.__init__(self, y) inside Z directly.我能想到的最后一种方法不是 (!) 使用 super 而直接在Z内部调用X.__init__(self, x)Y.__init__(self, y)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM