python 设计一个Circle(圆)类,包含半径、颜色等属性,且包括计算其周长和面积的方法。创建Circle类的实例对象,传入其半径和颜色,计算出该实例对象的周长和面积。
时间: 2024-02-12 11:02:38 浏览: 61
python 输入半径计算圆的周长和面积 示例
5星 · 资源好评率100%
好的,以下是Circle类的代码实现:
```python
import math
class Circle:
def __init__(self, radius, color):
self.radius = radius
self.color = color
def get_circumference(self):
return 2 * math.pi * self.radius
def get_area(self):
return math.pi * self.radius ** 2
# 创建Circle类的实例对象
my_circle = Circle(5, "red")
# 计算该实例对象的周长和面积
circumference = my_circle.get_circumference()
area = my_circle.get_area()
print("My circle has a radius of", my_circle.radius, "and a color of", my_circle.color)
print("Its circumference is", round(circumference, 2))
print("Its area is", round(area, 2))
```
运行结果如下:
```
My circle has a radius of 5 and a color of red
Its circumference is 31.42
Its area is 78.54
```
阅读全文