python, inheritance, super() method

江枫思渺然 提交于 2019-11-29 14:56:00

You need to pass in the Circle class instead:

text = super(Circle, self).ToString() + "{radius = " + str(self.radius) + "}\n"

super() will look through the base classes of the first argument to find the next ToString() method, and Point doesn't have a parent with that method.

With that change, the output is:

>>> print( shapeTwo.ToString() )
{x:0.0, y:0.0}
{radius = 12}

Note that you make the same mistake in your __init__; you are not calling the inherited __init__ at all. This works:

def __init__(self, x, y, radius):
    super(Circle, self).__init__(x ,y)
    self.radius = radius
    print("circle constructor")

and then the output becomes:

>>> shapeTwo = Circle(4, 6, 12)
point constructor
circle constructor
>>> print( shapeTwo.ToString() )
{x:4, y:6}
{radius = 12}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!