【題目描述】定義一個平面點類Point,對其重載運算符關系運算符,關系運算以距離坐標原點的遠近作為基准,遠的為大。
程序完成對其的測試。
【練習要求】請給出源代碼程序和運行測試結果,源代碼程序要求添加必要的注釋。
程序完成對其的測試。
【練習要求】請給出源代碼程序和運行測試結果,源代碼程序要求添加必要的注釋。
代碼:
import math class Point(): def __init__(self,x,y): self.x = x self.y = y def __lt__(self, other): l1 = math.sqrt(self.x**2+self.y**2) l2 = math.sqrt(other.x**2+other.y**2) return l1<l2 def __le__(self, other): l1 = math.sqrt(self.x**2+self.y**2) l2 = math.sqrt(other.x**2+other.y**2) return l1<=l2 def __gt__(self, other): l1 = math.sqrt(self.x**2+self.y**2) l2 = math.sqrt(other.x**2+other.y**2) return l1>l2 def __ge__(self, other): l1 = math.sqrt(self.x**2+self.y**2) l2 = math.sqrt(other.x**2+other.y**2) return l1>=l2 def __eq__(self, other): l1 = math.sqrt(self.x**2+self.y**2) l2 = math.sqrt(other.x**2+other.y**2) return l1==l2 def __ne__(self, other): l1 = math.sqrt(self.x**2+self.y**2) l2 = math.sqrt(other.x**2+other.y**2) return l1!=l2 p1 = Point(1,2) p2 = Point(3,4) p=p1<p2 print(p) p=p1<=p2 print(p) p=p1>p2 print(p) p=p1>=p2 print(p) p=p1==p2 print(p) p=p1!=p2 print(p)