事情是这样的:
假设我有这样一个需求:
给定三点:A 、B 、C ;其中 A 与 B 坐标已知,C 点坐标 = A 点 + B 点, 当 A 点坐标发生变化时,C 点也应该发生变化。
我的代码如下
首先我定义一个 Point 类
class Point:
def __init__(self, coordinate:tuple):
self.x = coordinate[0]
self.y = coordinate[1]
def __repr__(self):
return "(%.2f, %.2f)" % (self.x, self.y)
def __add__(self, other:Point):
if isinstance(other, Point):
x = self.x + other.x
y = self.y + other.y
return Point(coordinate=(x,y))
然后定义 A 与 B 的坐标 以及 C 的关系:
A = Point((1,1))
B = Point((4,1))
C = A + B
此时 C 点的坐标应该为:(5,2)
但我进行如下操作后
A.x=5