Python编写圆类,成员变量有圆心和半径,圆心是一个point变量
时间: 2024-03-09 10:51:41 浏览: 25
可以使用 Python 编写一个 Circle 圆类,其中包含圆心和半径两个成员变量,其中圆心是一个 Point 变量,具体实现如下:
```python
import math
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
class Circle:
def __init__(self, center, radius):
self.center = center
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
```
其中,Point 类表示一个点,包含了 x 和 y 两个属性。Circle 类包含了圆心和半径两个属性,以及三个方法:
- `get_area()`:获取圆的面积
- `get_circumference()`:获取圆的周长
- `get_diameter()`:获取圆的直径
可以通过以下方式创建一个 Circle 对象并调用其方法:
```python
# 创建一个圆心为 (1, 2),半径为 5 的圆
center = Point(1, 2)
c = Circle(center, 5)
# 获取圆的面积
area = c.get_area()
print(f"圆的面积为:{area}")
# 获取圆的周长
circumference = c.get_circumference()
print(f"圆的周长为:{circumference}")
# 获取圆的直径
diameter = c.get_diameter()
print(f"圆的直径为:{diameter}")
```
相关推荐
![jar](https://img-home.csdnimg.cn/images/20210720083455.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)