Python 点类:重载运算符实现点加法和距离计算
class Point: def init(self, x, y): self.x = x self.y = y
def __add__(self, other):
new_x = self.x + other.x
new_y = self.y + other.y
return Point(new_x, new_y)
def __sub__(self, other):
distance = ((self.x - other.x)**2 + (self.y - other.y)**2)**0.5
return distance
原文地址: https://www.cveoy.top/t/topic/kWek 著作权归作者所有。请勿转载和采集!