运算符重载
from math import hypot,sqrt
class Vector2D:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
"""
加法重载
:param other:
:return:
"""
x = self.x + other.x
y = self.y + other.y
return Vector2D(x, y)
def __str__(self):
return "Vector2D(x = {},y = {})".format(self.x, self.y)
def __sub__(self, other):
"""
减法重载
:param other:
:return:
"""
x = self.x - other.x
y = self.y - other.y
return Vector2D(x, y)
def __mul__(self, other):
"""
乘法重载
:param other:
:return:
"""
x = self.x * other.x
y = self.y * other.y
return Vector2D(x, y