1 回答
TA贡献1811条经验 获得超6个赞
是的,您必须覆盖方法__add__,__mul__并且__repr__:
from PyQt5.QtCore import QPointF
class Point(QPointF):
def dist(self):
return (self._p.x() * self._p.x() + self._p.y() * self._p.y()) ** 0.5
def __add__(self, other):
return self.__class__(super(self.__class__, self).__add__(other))
def __mul__(self, other):
return self.__class__(super(self.__class__, self).__mul__(other))
def __repr__(self):
return "{}({}, {})".format(self.__class__.__name__, self.x(), self.y())
if __name__ == '__main__':
a = Point(1, 2)
b = Point(2, 3)
print(a, type(a))
print(b, type(b))
print(a + b, type(a + b))
a += Point(10, 10)
print(a, type(a))
a += QPointF(10, 10)
print(a, type(a))
print(a*3, type(a*3))
print("a: {}".format(a))
l = [a, b]
print(l)
添加回答
举报