5.21

所学时间:2小时

代码行数:20

博客园数:1篇

)、设计并实现平面点类Point

题目描述

定义一个平面点类Point,对其重载运算符关系运算符,关系运算以距离坐标原点的远近作为基准,远的为大。程序完成对其的测试。

【源代码程序】

import math # 定义平面点类 class Point: def __init__(self, x, y): self.x = x self.y = y # 重载小于运算符 def __lt__(self, other): distance_self = math.sqrt(self.x**2 + self.y**2) distance_other = math.sqrt(other.x**2 + other.y**2) return distance_self < distance_other # 测试 point1 = Point(3, 4) point2 = Point(1, 2) if point1 < point2: print("point1比point2远") else: print("point2比point1远")

posted @ 2024-05-21 17:27  Snor9  阅读(14)  评论(0)    收藏  举报