实际上,带有参数的Python中的多重继承在没有指针的情况下无效

时间:2018-05-29 08:46:13

标签: python python-3.x

class First:
    def __init__(self, x):
        self.x = x
        print("first")

    def __str__(self):
        return " " + str(self.x)


class Second:
    def __init__(self, y):
          self.y = y
          print("second")

    def __str__(self):
        return " " + str(self.y)


class Third(First, Second):
    def __init__(self, x, y, z):
          Second(y)
          First(x)
          self.z = z
          print("third")

    def __str__(self):
        return Second.__str__(self) + " " + str(self.z)

o = Third(12, 6, 5)

1 个答案:

答案 0 :(得分:0)

我认为你想要的是:

class Third(First, Second):
    def __init__(self,x,y,z):
          Second.__init__(self, y)      # initializes Second subclass
          First.__init__(self, x)       # initializes First subclass
          self.z=z
          print("third")
    def __str__(self):
        return Second.__str__(self)+" "+ str(self.z)

现在你可以做到:

>>> o = Third(12,6,5)
second
first
third
>>> print(o)
 6 5
>>> print(o.x, o.y, o.z)
12 6 5