kekekkecoco 2021-12-09 22:06 采纳率: 100%

# python关于类的问题

• 写回答

#### 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)

``````
本回答被题主选为最佳回答 , 对您是否有帮助呢?
评论

• 系统已结题 12月17日
• 已采纳回答 12月9日
• 创建了问题 12月9日

#### 悬赏问题

• ¥15 C++ /QT 内存权限的判断函数列举
• ¥15 深度学习GFnet理解问题
• ¥15 单细胞小提琴堆叠图代码
• ¥80 LS dyna mpp并行报错
• ¥50 升级strust2版本到2.3.15.1后使用ognl3.0.6.jar windows环境中没有问题，但部署到linux环境报错
• ¥15 vue页面，node封装接口
• ¥15 求TMS320F280039C工程模板！
• ¥15 delphi+fastreport实现分组补空打印问题
• ¥15 使用python把两台mysql数据库服务器数据导出和导入
• ¥15 NodeBB论坛配置Apache Solr中文搜索引擎的详细教程