定义一个圆形使用Circle类,半径为r,并且get/set方法,showArea方法打印圆形面积,showPerimeter方法打印圆形周长。
时间: 2024-05-12 18:21:04 浏览: 95
以下是Python的实现代码:
```python
import math
class Circle:
def __init__(self, r):
self.__radius = r
def get_radius(self):
return self.__radius
def set_radius(self, r):
self.__radius = r
def show_area(self):
area = math.pi * self.__radius ** 2
print("Circle area: {:.2f}".format(area))
def show_perimeter(self):
perimeter = 2 * math.pi * self.__radius
print("Circle perimeter: {:.2f}".format(perimeter))
```
使用示例:
```python
c = Circle(5)
c.show_area() # 输出:Circle area: 78.54
c.show_perimeter() # 输出:Circle perimeter: 31.42
c.set_radius(7)
c.show_area() # 输出:Circle area: 153.94
c.show_perimeter() # 输出:Circle perimeter: 43.98
```
阅读全文