当前位置: 代码网 > it编程>前端脚本>Python > python设计并实现平面点类Point的源代码

python设计并实现平面点类Point的源代码

2024年05月28日 Python 我要评论
【题目描述】定义一个平面点类point,对其重载运算符关系运算符,关系运算以距离坐标原点的远近作为基准,远的为大。程序完成对其的测试。【源代码程序】import mathclass 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设计并实现平面点类point的源代码的文章就介绍到这了,更多相关python平面点类point内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com