构造函数:超级与类的使用.__ init __()?

时间:2017-01-05 17:46:13

标签: python inheritance constructor init super

A类:         def init (self,x,y):             self.x = x             self.y = y

    def to_string(self):
        return "x, y = (" + str(self.x) + ", " + str(self.y) + ")"

    def __str__(self):
        return self.to_string()

class B(A):
    def __init__(self, z, *args):
        A.__init__(self, *args)
        self.z = z

    def __str__(self):
        return super().__str__()

class C(A):
    def __init__(self, *args):
        super().__init__(*args)

    def to_string(self):
        return "Klasse C, " + super().to_string()  

为什么可以在C类中初始化A的实例而不将“self”作为参数传递,而在B类中我们需要将“self”作为参数传递以使代码正常运行?

提前感谢您解决此问题的任何帮助

0 个答案:

没有答案