python.38课习题,求两点之间的距离,感觉源程序画蛇添足了,求详解
import mathclass Point():
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def getX(self): #getX方法和getY方法,感觉非常多余
return self.x
def getY(self):
return self.y
class Line():
def __init__(self, p1, p2):
self.x = p1.getX() - p2.getX() #此处直接访问Point类的属性不行吗?为什么要绕一圈调用Point类中的两个方法呢?
self.y = p1.getY() - p2.getY()
self.len = math.sqrt(self.x*self.x + self.y*self.y)
def getLen(self):
return self.len
>>> p1 = Point(1, 1)
>>> p2 = Point(4, 5)
>>> line = Line(p1, p2)
>>> line.getLen()
5.0
实践是检验真理的唯一标准,大胆去做咯
import math
class Point():
def __init__(self, x=0, y=0):
self.x = x
self.y = y
class Line():
def __init__(self, p1, p2):
self.x = p1.x - p2.x
self.y = p1.y - p2.y
self.len = math.sqrt(self.x*self.x + self.y*self.y)
def getLen(self):
return self.len
>>> p1 = Point(1, 1)
>>> p2 = Point(4, 5)
>>> line = Line(p1, p2)
>>> line.getLen()
5.0
页:
[1]