|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
抄的一个面向对象编程基础的代码。。。求两点距离的。
中间部分的 :
def distance_to(self,other):
dx = self.x - other.x
dy = self.y - other.y
这个other.x 是什么意思???搞不懂...
###########
import math
class point(object):
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,dx,dy):
self.x += dx
self.y += dy
print(dx,dy,"555")
def distance_to(self,other):
dx = self.x - other.x
dy = self.y - other.y
print(other.x,other.y,self.x,self.y,"666")
return math.sqrt(dx**2 + dy**2)
def __str__(self):
return "{},{}".format(str(self.x),str(self.y))
def main():
p1 = point(3,5)
p2 = point()
print(p1,p2)
p2.move_by(-1,2)
print(p2)
print(p1.distance_to(p2))
if __name__ == "__main__":
main() |
|