、设计一个Circle(圆)类,包括半径、颜色等属性,且包括计算其周长和面积的方法。创建Circle类的实例对象,传入其半径和颜色,计算出该实例对象的周长和面积
时间: 2023-06-10 11:08:45 浏览: 127
c++—用类进行圆的周长和面积的计算
以下是一个基本的实现:
```python
import math
class Circle:
def __init__(self, radius, color):
self.radius = radius
self.color = color
def circumference(self):
return 2 * math.pi * self.radius
def area(self):
return math.pi * self.radius ** 2
# 创建Circle类的实例对象
my_circle = Circle(5, "red")
# 计算出该实例对象的周长和面积
circumference = my_circle.circumference()
area = my_circle.area()
print(f"The circumference of my circle is {circumference:.2f}")
print(f"The area of my circle is {area:.2f}")
```
运行结果:
```
The circumference of my circle is 31.42
The area of my circle is 78.54
```
阅读全文