把C和AB关联起来就可以了
class Point:
def __init__(self, coordinate:tuple,point = None,point2 = None):
self._point = point
self._point2 = point2
if self._point is None or self._point2 is None:
self._point,self._point2 = coordinate
def _simple(self):
return not isinstance(self._point, Point)
def _reset(self,x,y):
self._point = x
self._point2 = y
def x(self,vx = None):
if vx is not None: self._reset(vx,self.y())
return self._point if self._simple() else self._point.x() + self._point2.x()
def y(self,vy = None):
if vy is not None: self._reset(self.x(),vy)
return self._point2 if self._simple() else self._point.y() + self._point2.y()
def __add__(self, other):
return Point(None, self, other) if isinstance(other, Point) else self
def __repr__(self):
return "(%.2f, %.2f)" % (self.x(), self.y())
A = Point((1,1))
B = Point((4,1))
C = A + B
print(C) #(5,2)
A.x(5)
print(C) #(9,2)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…