私はPythonを初めて使用します。以下のコードを使用すると、動作しなくなります。-これは継承です。サークル基本クラスがあり、circle
クラス内でこれを継承します(ここでは単一の継承のみ)。
問題はクラス内のToString()
関数、特に
少なくとも1つの引数を必要とする行にあることを理解していますが、次のようになります。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