【发布时间】:2020-05-23 08:29:02
【问题描述】:
我有一个 Python 类,它可以让你用两个 x-y 坐标点来初始化线条。我还有一个将两行加在一起的功能,如下所示:
class Line:
def __init__(self, x1, y1, x2, y2):
...
def __add__(self, other):
new_x1 = self.x1 + other.x1
new_y1 = self.y1 + other.y1
new_x2 = self.x2 + other.x2
new_y2 = self.y2 + other.y2
return Line(new_x1, new_y1, new_x2, new_y2)
我有非常相似的减法和乘法函数,它们与 add 函数相同,但内部有不同的运算符符号。
我的问题是,有没有更好或更有效的方法来写这个?
【问题讨论】:
-
为什么
Line类中的重载加法返回Point对象?两个Point项相加是什么意思? (除了仿射几何,你不用加点) -
抱歉,打错了。它应该是线
标签: python function class operator-overloading