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)

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM