Python-100-Days/Day01-15/code/Day09/association.py

72 lines
1.3 KiB
Python
Raw Normal View History

2018-04-27 00:00:22 +08:00
"""
对象之间的关联关系
Version: 0.1
Author: 骆昊
Date: 2018-03-12
"""
from math import sqrt
class Point(object):
2019-05-03 21:17:36 +08:00
def __init__(self, x=0, y=0):
self._x = x
self._y = y
2018-04-27 00:00:22 +08:00
2019-05-03 21:17:36 +08:00
def move_to(self, x, y):
self._x = x
self._y = y
2018-04-27 00:00:22 +08:00
2019-05-03 21:17:36 +08:00
def move_by(self, dx, dy):
self._x += dx
self._y += dy
2018-04-27 00:00:22 +08:00
2019-05-03 21:17:36 +08:00
def distance_to(self, other):
dx = self._x - other._x
dy = self._y - other._y
return sqrt(dx ** 2 + dy ** 2)
2018-04-27 00:00:22 +08:00
2019-05-03 21:17:36 +08:00
def __str__(self):
return '(%s, %s)' % (str(self._x), str(self._y))
2018-04-27 00:00:22 +08:00
class Line(object):
2019-05-03 21:17:36 +08:00
def __init__(self, start=Point(0, 0), end=Point(0, 0)):
self._start = start
self._end = end
2018-04-27 00:00:22 +08:00
2019-05-03 21:17:36 +08:00
@property
def start(self):
return self._start
2018-04-27 00:00:22 +08:00
2019-05-03 21:17:36 +08:00
@start.setter
def start(self, start):
self._start = start
2018-04-27 00:00:22 +08:00
2019-05-03 21:17:36 +08:00
@property
def end(self):
return self.end
2018-04-27 00:00:22 +08:00
2019-05-03 21:17:36 +08:00
@end.setter
def end(self, end):
self._end = end
2018-04-27 00:00:22 +08:00
2019-05-03 21:17:36 +08:00
@property
def length(self):
return self._start.distance_to(self._end)
2018-04-27 00:00:22 +08:00
if __name__ == '__main__':
2019-05-03 21:17:36 +08:00
p1 = Point(3, 5)
print(p1)
p2 = Point(-2, -1.5)
print(p2)
line = Line(p1, p2)
print(line.length)
line.start.move_to(2, 1)
line.end = Point(1, 2)
print(line.length)