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 如图所示交换机网络该如何规划配置
• ¥15 CUDA driver error
• ¥15 Dijkstra 算法的堆优化方法
• ¥15 师哥师姐们，如何帮我下载一下python？
• ¥15 Office版本升级，Oracle连接报错
• ¥20 利用python搜索PDF文件中是否存在1
• ¥15 ImportPathMismatchError
• ¥15 滤波器这个效果是对的么
• ¥15 c++静态函数的静态函数参数问题
• ¥15 51单片机数码管显示有问题