3.创建圆环类的对象,并输出对象的面积和周长;创建多个圆,输出总的个数,每个圆的面积周长等。
时间: 2023-05-19 17:06:31 浏览: 56
用面向对象的方法,设计一个圆的类,包括计算圆的直径、圆的周长和圆的面积的方法。
好的,我可以回答这个问题。以下是代码示例:
```python
class Circle:
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * self.radius ** 2
def perimeter(self):
return 2 * 3.14 * self.radius
# 创建圆环类的对象
circle1 = Circle(5)
circle2 = Circle(7)
# 输出对象的面积和周长
print("Circle 1 area:", circle1.area())
print("Circle 1 perimeter:", circle1.perimeter())
print("Circle 2 area:", circle2.area())
print("Circle 2 perimeter:", circle2.perimeter())
# 创建多个圆
circles = [Circle(3), Circle(4), Circle(6)]
# 输出总的个数,每个圆的面积周长等
total_circles = len(circles)
total_area = sum([circle.area() for circle in circles])
total_perimeter = sum([circle.perimeter() for circle in circles])
print("Total circles:", total_circles)
print("Total area:", total_area)
print("Total perimeter:", total_perimeter)
```
希望这个代码示例对你有所帮助!
阅读全文