请用接口实现以下UML图 测试类中,有三个柱体,分别以三角形,圆形,矩形为底面积,求它们的体积。 测试用例::三角形边:(3,4,5);圆形半径:2; 矩形边(3,4),柱体高:6. 2.用匿名类实现第一题 ;
时间: 2024-06-13 13:08:21 浏览: 11
```python
# 定义抽象类Geometry
from abc import ABC, abstractmethod
class Geometry(ABC):
@abstractmethod
def getArea(self):
pass
# 定义Circle类,继承Geometry类
class Circle(Geometry):
def __init__(self, r):
self.r = r
def getArea(self):
return 3.14 * self.r * self.r
# 定义Triangle类,继承Geometry类
class Triangle(Geometry):
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
def getArea(self):
p = (self.a + self.b + self.c) / 2
return (p * (p - self.a) * (p - self.b) * (p - self.c)) ** 0.5
# 定义Rectangle类,继承Geometry类
class Rectangle(Geometry):
def __init__(self, l, w):
self.l = l
self.w = w
def getArea(self):
return self.l * self.w
# 定义测试类
class Test:
def test(self):
# 创建三个对象
circle = Circle(2)
triangle = Triangle(3, 4, 5)
rectangle = Rectangle(3, 4)
# 计算体积
cylinder1 = circle.getArea() * 6
cylinder2 = triangle.getArea() * 6
cylinder3 = rectangle.getArea() * 6
# 输出结果
print("圆柱体积为:", cylinder1)
print("三角形柱体积为:", cylinder2)
print("矩形柱体积为:", cylinder3)
# 创建Test对象并调用test方法
t = Test()
t.test()
```
--相关问题--:
1. 什么是抽象类和抽象方法?
2. Python中如何
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)