设计一个基类shape。该类包含3个属性:图形名称(name)、图形面积(area)和图形周长(perimeter)。该类包含3个成员函数:calArea()—计算并返回该图形的面积;calPerimeter()—计算并返回该图形的周长;display()—打印输出图形名称、面积、周长。设计3个派生类:Rectangle、Triangle、Circle,请为派生类定义合适的数据成员用于表达其结构,派生类应重载实现基类中的全部成员函数。
1条回答 默认 最新
- 索利亚噶通 2021-12-09 22:23关注
有用请采纳
class Shape: def __init__(self, name): self.name = name self.area = 0.0 # 面积 self.perimeter = 0.0 # 周长 def calArea(self): return self.area def calPerimeter(self): return self.perimeter def display(self): print("name = ", self.name) print("area = ", self.area) print("perimeter = ", self.perimeter) class Rectangle(Shape): def __init__(self, name, width, height): # width为宽, height为高 Shape.__init__(self, name) self.width = width self.height = height self.area = self.calArea() self.perimeter = self.calPerimeter() def calArea(self): return self.width * self.height def calPerimeter(self): return (self.width + self.height) * 2 def display(self): print("name = ", self.name) print("area = ", self.area) print("perimeter = ", self.perimeter) class Triangle(Shape): def __init__(self, name, a, b, c, height): # a, b, c为3边, height为a对应的高 Shape.__init__(self, name) self.a = a self.b = b self.c = c self.height = height self.area = self.calArea() self.perimeter = self.calPerimeter() def calArea(self): return self.a * self.height / 2 def calPerimeter(self): return self.a + self.b + self.c def display(self): print("name = ", self.name) print("area = ", self.area) print("perimeter = ", self.perimeter) class Circle(Shape): def __init__(self, name, r): # r为半径 , 取PI = 3.14 Shape.__init__(self, name) self.r = r self.area = self.calArea() self.perimeter = self.calPerimeter() def calArea(self): return self.r * self.r * 3.14 def calPerimeter(self): return 2 * 3.14 * self.r def display(self): print("name = ", self.name) print("area = ", self.area) print("perimeter = ", self.perimeter)
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 5无用
悬赏问题
- ¥15 乌班图ip地址配置及远程SSH
- ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
- ¥15 PSPICE制作一个加法器
- ¥15 javaweb项目无法正常跳转
- ¥15 VMBox虚拟机无法访问
- ¥15 skd显示找不到头文件
- ¥15 机器视觉中图片中长度与真实长度的关系
- ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
- ¥15 java 的protected权限 ,问题在注释里
- ¥15 这个是哪里有问题啊?