我是 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 constructor
def __init__(self, x, y, radius):
super(Point, self) #super().__init__(x,y)
self.radius = radius
print("circle constructor")
def ToString(self):
text = super(Point, self).ToString() + "{radius = " + str(self.radius) + "}\n"
return text
shapeOne = Point(10,10)
print( shapeOne.ToString() ) # this works fine
shapeTwo = Circle(4, 6, 12)
print( shapeTwo.ToString() ) # does not work