-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathex53.py
More file actions
61 lines (48 loc) · 1.47 KB
/
ex53.py
File metadata and controls
61 lines (48 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/home/wizard/anaconda3/bin/python
class Point():
count = 0
# Конструктор на класа
def __init__(self, x=0, y=0):
# Данни на обекта
self.__x = x
self.__y = y
Point.count += 1
# Методи на класа
def moveTo(self, dx,dy):
self.__x += dx
self.__y += dy
def show(self):
print(f'Draw Point:({self.__x},{self.__y}) Count:{Point.count}')
# Специални методи
def __add__(self,other):
""" p = p1 + p2 """
if not isinstance(other,Point):
return NotImplemented
return Point( self.__x + other.__x, self.__y + other.__y)
def __iadd__(self,other):
""" p1 = p1 + p2 in-place addition"""
# print('in-place addition')
if not isinstance(other,Point):
return NotImplemented
self.__x += other.__x
self.__y += other.__y
return self
def __str__(self):
"""str(obj) => string"""
return f'({self.__x},{self.__y})'
def __eq__(self,other):
""" if p1 == p2: ... """
if not isinstance(other,Point):
return NotImplemented
#return abs(self.__x - other.__x)< 10 ...
return self.__x == other.__x and self.__y == other.__y
def __del__(self):
"""finalize()"""
Point.count -= 1
#
if __name__ == '__main__':
p1 = Point(4,5)
p2 = Point(7,8)
p1.show()
del p1
p2.show()