定义一个Point类,有点坐标属性(x,y),再定义一个矩形Rectangle类,其属性为两个点Point点,以及含有计算矩形的周长及面积的成员函数
时间: 2024-04-29 19:26:10 浏览: 156
类Point_point类_
好的,下面是代码示例:
```python
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
class Rectangle:
def __init__(self, point1, point2):
self.point1 = point1
self.point2 = point2
def get_perimeter(self):
width = abs(self.point1.x - self.point2.x)
height = abs(self.point1.y - self.point2.y)
return 2 * (width + height)
def get_area(self):
width = abs(self.point1.x - self.point2.x)
height = abs(self.point1.y - self.point2.y)
return width * height
```
使用示例:
```python
# 创建两个点对象
p1 = Point(1, 2)
p2 = Point(4, 6)
# 创建一个矩形对象
rect = Rectangle(p1, p2)
# 计算矩形的周长和面积
print(rect.get_perimeter()) # 输出: 12
print(rect.get_area()) # 输出: 12
```
希望这个回答能够解决您的问题!
阅读全文