请用接口实现以下UML图 测试类中,有三个柱体,分别以三角形,圆形,矩形为底面积,求它们的体积。 测试用例::三角形边:(3,4,5);圆形半径:2; 矩形边(3,4),柱体高:6.
时间: 2024-06-13 19:08:43 浏览: 58
```python
from abc import ABC, abstractmethod
import math
class Geometry(ABC):
@abstractmethod
def getArea(self):
pass
class Circle(Geometry):
def __init__(self, radius):
self.radius = radius
def getArea(self):
return math.pi * self.radius ** 2
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 math.sqrt(p * (p - self.a) * (p - self.b) * (p - self.c))
class Rectangle(Geometry):
def __init__(self, length, width):
self.length = length
self.width = width
def getArea(self):
return self.length * self.width
class Cylinder:
def __init__(self, bottom, height):
self.bottom = bottom
self.height = height
def getVolume(self):
return self.bottom.getArea() * self.height
circle = Circle(2)
triangle = Triangle(3, 4, 5)
rectangle = Rectangle(3, 4)
cylinder1 = Cylinder(circle, 6)
cylinder2 = Cylinder(triangle, 6)
cylinder3 = Cylinder(rectangle, 6)
print("圆柱1的体积为:", cylinder1.getVolume())
print("圆柱2的体积为:", cylinder2.getVolume())
print("圆柱3的体积为:", cylinder3.getVolume())
```
阅读全文