小编use*_*226的帖子

python,继承,super()方法

我是python的新手,我有下面的代码,我无法开始工作: - 这是继承,我有一个圆基类,我在一个circle类中继承它(这里只是单继承).

我理解这个问题是在类中的ToString()函数内circle,特别是行,text = super(Point, self).ToString() +.. 它至少需要一个参数,但我得到了这个:

AttributeError: 'super' object has no attribute 'ToString'

我知道super没有ToString属性,但Point课程确实 -

我的代码:

class Point(object):
    x = 0.0
    y = 0.0

    # point class constructor
    def __init__(self, x, y):
        self.x = x
        self.y = y
        print("point constructor")

    def ToString(self):
        text = "{x:" + str(self.x) + ", y:" + str(self.y) + "}\n"
        return text

class Circle(Point):
    radius = 0.0

    # circle class …
Run Code Online (Sandbox Code Playgroud)

python inheritance super

3
推荐指数
1
解决办法
4612
查看次数

标签 统计

inheritance ×1

python ×1

super ×1