设计一个基类Shape,包括: 名为sName的属性(图形名称); 构造函数应对sName属性进行初始化。 设计Shape的继承类Rectangle, 包括: 长,宽两个属性; 构造函数调用Shape的构造函数,并初始化长,宽两个属性; getArea()成员函数计算并返回矩形面积。 设计Shape的继承类Circle,包括: 半径属性; 构造函数调用Shape的构造函数,并初始化半径属性; getArea()成员函数计算并返回圆形面积。 注意:请结合程序以理解题目对类的接口的要求。 【样例输入】 Jupyter 12.1 9.9 Moon 3.3 【样例输出】 shape0 Rect Jupyter,Area: 119.79 Circle Moon,Area: 34.21 s1 = Shape("shape0") s = input() #Name of Rectangle w = float(input()) #Width of Rect h = float(input()) #Height of Rect r1 = Rectangle(s,w,h) s = input() #Name of Circle r = float(input()) #Radius of Circle c1 = Circle(s,r) print(s1.sName) print("Rect %s,Area: %.2f" % (r1.sName,r1.getArea())) print("Circle %s,Area: %.2f" % (c1.sName,c1.getArea()))
时间: 2024-03-17 13:46:53 浏览: 24
以下是基于题目要求的 Python 代码实现:
```
import math
class Shape:
def __init__(self, name):
self.sName = name
class Rectangle(Shape):
def __init__(self, name, width, height):
super().__init__(name)
self.width = width
self.height = height
def getArea(self):
return self.width * self.height
class Circle(Shape):
def __init__(self, name, radius):
super().__init__(name)
self.radius = radius
def getArea(self):
return math.pi * self.radius ** 2
s1 = Shape("shape0")
s = input() # Name of Rectangle
w = float(input()) # Width of Rect
h = float(input()) # Height of Rect
r1 = Rectangle(s, w, h)
s = input() # Name of Circle
r = float(input()) # Radius of Circle
c1 = Circle(s, r)
print(s1.sName)
print("Rect %s,Area: %.2f" % (r1.sName, r1.getArea()))
print("Circle %s,Area: %.2f" % (c1.sName, c1.getArea()))
```
程序的输入格式为:首先输入一个长方形的名称,然后输入长方形的宽和高,接下来输入一个圆形的名称和半径。程序的输出格式为:输出基类Shape的名称,以及长方形和圆形的名称和面积,保留两位小数。