CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
jackfrued

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: jackfrued/Python-100-Days
Path: blob/master/Day01-15/code/Day09/association.py
Views: 729
1
"""
2
对象之间的关联关系
3
4
Version: 0.1
5
Author: 骆昊
6
Date: 2018-03-12
7
"""
8
9
from math import sqrt
10
11
12
class Point(object):
13
14
def __init__(self, x=0, y=0):
15
self._x = x
16
self._y = y
17
18
def move_to(self, x, y):
19
self._x = x
20
self._y = y
21
22
def move_by(self, dx, dy):
23
self._x += dx
24
self._y += dy
25
26
def distance_to(self, other):
27
dx = self._x - other._x
28
dy = self._y - other._y
29
return sqrt(dx ** 2 + dy ** 2)
30
31
def __str__(self):
32
return '(%s, %s)' % (str(self._x), str(self._y))
33
34
35
class Line(object):
36
37
def __init__(self, start=Point(0, 0), end=Point(0, 0)):
38
self._start = start
39
self._end = end
40
41
@property
42
def start(self):
43
return self._start
44
45
@start.setter
46
def start(self, start):
47
self._start = start
48
49
@property
50
def end(self):
51
return self.end
52
53
@end.setter
54
def end(self, end):
55
self._end = end
56
57
@property
58
def length(self):
59
return self._start.distance_to(self._end)
60
61
62
if __name__ == '__main__':
63
p1 = Point(3, 5)
64
print(p1)
65
p2 = Point(-2, -1.5)
66
print(p2)
67
line = Line(p1, p2)
68
print(line.length)
69
line.start.move_to(2, 1)
70
line.end = Point(1, 2)
71
print(line.length)
72
73