python面向对象设计一个圆类
时间: 2023-10-16 21:27:15 浏览: 47
好的,下面是 Python 中的圆类的面向对象设计:
```python
import math
class Circle:
def __init__(self, radius):
self.radius = radius
def get_area(self):
return math.pi * self.radius ** 2
def get_circumference(self):
return 2 * math.pi * self.radius
def get_diameter(self):
return 2 * self.radius
```
这里定义了一个名为 `Circle` 的类,该类具有以下属性:
- `radius`:圆的半径,作为类的构造函数参数
- `get_area()`:计算并返回圆的面积
- `get_circumference()`:计算并返回圆的周长
- `get_diameter()`:计算并返回圆的直径
使用示例:
```python
circle = Circle(5)
print("圆的半径:", circle.radius)
print("圆的面积:", circle.get_area())
print("圆的周长:", circle.get_circumference())
print("圆的直径:", circle.get_diameter())
```
输出结果如下:
```
圆的半径: 5
圆的面积: 78.53981633974483
圆的周长: 31.41592653589793
圆的直径: 10
```