6-1 设计一个Triangle类继承自GeometricObject类
时间: 2024-02-25 22:55:26 浏览: 169
好的,根据您的要求,我来设计一个Triangle类继承自GeometricObject类,以下是示例代码:
```python
class GeometricObject:
def __init__(self, color="white", filled=False):
self.color = color
self.filled = filled
def get_color(self):
return self.color
def set_color(self, color):
self.color = color
def is_filled(self):
return self.filled
def set_filled(self, filled):
self.filled = filled
class Triangle(GeometricObject):
def __init__(self, side1=1.0, side2=1.0, side3=1.0, color="white", filled=False):
super().__init__(color, filled)
self.side1 = side1
self.side2 = side2
self.side3 = side3
def get_side1(self):
return self.side1
def set_side1(self, side1):
self.side1 = side1
def get_side2(self):
return self.side2
def set_side2(self, side2):
self.side2 = side2
def get_side3(self):
return self.side3
def set_side3(self, side3):
self.side3 = side3
def get_area(self):
s = (self.side1 + self.side2 + self.side3) / 2
return (s * (s - self.side1) * (s - self.side2) * (s - self.side3)) ** 0.5
def get_perimeter(self):
return self.side1 + self.side2 + self.side3
```
这个Triangle类继承自GeometricObject类,具有三个属性:side1、side2和side3表示三角形的三边长度,还有color和filled属性表示三角形的颜色和是否填充。此外,Triangle类还实现了计算三角形面积和周长的方法get_area和get_perimeter。
阅读全文