编写程序创建Point类要求如下:一个名为distancedouble xdouble y的方法返回从该点到指定x和y坐标的指定点之间的距离
以下是Python实现:
class Point: def init(self, x, y): self.x = x self.y = y
def distance(self, x2, y2):
return ((self.x - x2) ** 2 + (self.y - y2) ** 2) ** 0.5
测试
p = Point(0, 0) print(p.distance(3, 4)) # 输出5.0
原文地址: https://www.cveoy.top/t/topic/fs1g 著作权归作者所有。请勿转载和采集!