python設計并實現(xiàn)平面點類Point的源代碼
更新時間:2024年05月08日 08:54:28 作者:不會JAVA的小袁
這篇文章主要介紹了python-設計并實現(xiàn)平面點類Point,定義一個平面點類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設計并實現(xiàn)平面點類Point的源代碼的文章就介紹到這了,更多相關python平面點類Point內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python深度學習實戰(zhàn)PyQt5基本控件使用解析
PyQt5 提供了豐富的輸入輸出控件。本文介紹通過 QtDesigner 工具欄創(chuàng)建常用的基本控件,包括各種按鈕控件、文本輸入控件和調節(jié)輸入控件2021-10-10
python實現(xiàn)登陸知乎獲得個人收藏并保存為word文件
這篇文章主要介紹了python實現(xiàn)登陸知乎獲得個人收藏并保存為word文件,本文直接給出實現(xiàn)代碼,需要的朋友可以參考下2015-03-03
Python爬蟲模擬登陸嗶哩嗶哩(bilibili)并突破點選驗證碼功能
這篇文章主要介紹了Python爬蟲模擬登陸嗶哩嗶哩(bilibili)并突破點選驗證碼功能,本文通過圖文實例相結合給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12
Python實現(xiàn)識別手寫數(shù)字 Python圖片讀入與處理
這篇文章主要為大家詳細介紹了Python實現(xiàn)識別手寫數(shù)字,Python圖片的讀入與處理,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01

