定义一个名为Triangle的三角形类,使其继承Shape抽象类,覆盖Shape类中的抽象方法perimeter()和area()。编写程序测试Triangle类的使用。
时间: 2023-06-06 16:04:54 浏览: 108
好的,我明白了您的要求。以下是Triangle类的定义和测试程序:
```python
# Shape抽象类定义
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
@abstractmethod
def perimeter(self):
pass
# Triangle类
class Triangle(Shape):
def __init__(self, base, height, side1, side2):
self.base = base
self.height = height
self.side1 = side1
self.side2 = side2
def area(self):
return 0.5 * self.base * self.height
def perimeter(self):
return self.base + self.side1 + self.side2
# 测试程序
t = Triangle(3, 4, 5, 6)
print("Triangle area: ", t.area())
print("Triangle perimeter: ", t.perimeter())
```
输出结果为:
```
Triangle area: 6.0
Triangle perimeter: 14
```
阅读全文