from math import sqrt
class Point:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def move_to(self, x, y):
"""
移动到新的坐标
"""
self.x = x
self.y = y
def move_by(self, bx, by):
"""
移动指定的增量
"""
self.x += bx
self.y += by
def distance_to(self, other):
dx = self.x - other.x
dy = self.y - other.y
return sqrt(dx ** 2 + dy ** 2)
def __str__(self):
"""
打印一个实例化对象时,打印的其实时一个对象的地址。
而通过__str__()函数就可以帮助我们打印对象中具体的属性值,
或者你想得到的东西
"""
return ("%s, %s") % (str(self.x), str(self.y))
def main():
p1 = Point()
p2 = Point(3,4)
print("p1: ", p1, "p2: ", p2)
p2.move_to(4, 5)
print(p2)
p2.move_by(-1,-1)
print(p2)
distance = p2.distance_to(p1)
print(distance)
if __name__ == '__main__':
main()