创建类MyMath,计算圆的周长和面积以及球的表面积和体积,并编写测试代码,结果均保留俩位小数,用python语言
时间: 2023-06-05 07:04:30 浏览: 497
好的,这是一个纯数学问题,可以回答。以下是代码:
```python
import math
class MyMath:
@staticmethod
def circle_area(radius):
return round(math.pi * radius**2, 2)
@staticmethod
def circle_circumference(radius):
return round(2 * math.pi * radius, 2)
@staticmethod
def sphere_surface_area(radius):
return round(4 * math.pi * radius**2, 2)
@staticmethod
def sphere_volume(radius):
return round(4/3 * math.pi * radius**3, 2)
# 测试代码
if __name__ == '__main__':
radius = 3
print(f"圆的周长:{MyMath.circle_circumference(radius)}")
print(f"圆的面积:{MyMath.circle_area(radius)}")
print(f"球的表面积:{MyMath.sphere_surface_area(radius)}")
print(f"球的体积:{MyMath.sphere_volume(radius)}")
```
运行结果:
```
圆的周长:18.85
圆的面积:28.27
球的表面积:113.1
球的体积:113.1
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](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)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)