所以我需要找到矩形的面积。我必须在 Rectangle 类中放置一个名为“CalcArea()”的方法,该方法乘以宽度 * 高度。我不知道如何做到这一点,而且我很迷茫,这本书根本没有解释如何做到这一点。这是我的代码:
class Point():
x = 0.0
y = 0.0
def __init__(self, x, y):
self.x = x
self.y = y
print("Point Constructor")
def ToString(self):
return "{X:" + str(self.x) + ",Y:" + str(self.y) + "}"
class Ellipse(Point):
radiusV = 0.0
radiusH = 0.0
def __init__(self, x, y, radiusV, radiusH):
super().__init__(x,y)
self.radiusV = radiusV
self.radiusH = radiusH
print("Ellipse Constructor")
def ToString(self):
return super().ToString() + \
",{Radius: Vertical = " + str(self.radiusV) + ", Radius: Horizontal = " + str(self.radiusH) + "}"
p = Point(50,50)
print(p.ToString())
e = Ellipse(80,80,60,80)
print(e.ToString())
class Size():
width = 0.0
height = 0.0
def __init__(self, width, height):
self.width = width
self.height = height
print ("Size Constructor")
def ToString(self):
return "{Width = " + str(self.width) + \
", Height = " + str(self.height) + "}"
class Rectangle(Point, Size):
area = 0.0
def __init__(self, x, y, width, height):
Point.__init__(self,x,y)
Size.__init__(self,width,height)
print("Rectangle Constructor")
def ToString(self):
return Point.ToString(self) + "," + Size.ToString(self) +
s = Size (80,70)
print(s.ToString())
r = Rectangle(200,250,40,50)
print(r.ToString())