python-设计并实现平面点类Point
【题目描述】定义一个平面点类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)
标签:
python
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!