# 第25章示例:向量类,运算符重载 __add__

class Vector:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __add__(self, other):
        # 对应坐标相加,返回新向量(不改原对象)
        return Vector(self.x + other.x, self.y + other.y)

    def __eq__(self, other):
        if not isinstance(other, Vector):
            return False
        return self.x == other.x and self.y == other.y

    def __str__(self):
        return f"({self.x}, {self.y})"


v1 = Vector(1, 2)
v2 = Vector(3, 4)
v3 = v1 + v2            # 触发 __add__!
print(v3)               # (4, 6)

print(v1 == Vector(1, 2))   # True(坐标相同)
print(v1 == v2)             # False
