1
我是新来的python,我有下面的代码,我只是无法去工作: - 这是继承,我有一个圆形基类,我继承了这个一个circle
类(这里只是单一的继承)。python,inheritance,super()方法
我理解的问题是circle
类中ToString()
函数中,具体的线路,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
非常感谢这一步,但不是想要的结果,所以我觉得我已经做了别的事情错在这里(!)。我现在得到这 - 点构造 {X:10,Y:10} 圈构造 {X:0.0,Y:0.0} {半径= 12} – user1937226
当我的圆目的是,shapeTwo =圈(4, 6,12) – user1937226
所以我应该问,我如何通过从点到点的x,y点? – user1937226