I'm new to python, I have the code below which I just can't get to work:-
This is inheritance, I have a circle base class and I inherit this within a circle
class (just single inheritance here).
I understand the issue is within the ToString()
function within the circle
class, specifically the line, text = super(Point, self).ToString() +..
which requires at least a single argument, yet I get this:
AttributeError: 'super' object has no attribute 'ToString'
I know super
has no ToString
attribute, but the Point
class does -
My code:
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) + "}
"
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) + "}
"
return text
shapeOne = Point(10,10)
print( shapeOne.ToString() ) # this works fine
shapeTwo = Circle(4, 6, 12)
print( shapeTwo.ToString() ) # does not work
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…