鱼C论坛

 找回密码
 立即注册
查看: 886|回复: 3

面向对象编程基础求助

[复制链接]
发表于 2019-5-14 23:23:06 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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()
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-5-14 23:32:51 | 显示全部楼层
本帖最后由 jackz007 于 2019-5-14 23:36 编辑

    显然 other 是另外一个 point 类对象,例如,
  1. p1 = point()
  2. p2 = point(3 , 5)
  3. x = p1 . distance_to(p2)  # 计算 p1(0 , 0) ~ p2(3 , 5) 之间的距离。
  4. x = p2 . distance_to(p1)  # 计算 p1(0 , 0) ~ p2(3 , 5) 之间的距离。
复制代码

    上面 2 个 x 的表达式作用是一样的,算出的数值也应该是相等的。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-14 23:36:57 | 显示全部楼层
懂了。。。 我好蠢 _(:з」∠)_
p1=point(3,5)
p2=point(0,0)
print(p1.distance_to(p2)) # other就是 point(0,0)里面的(0,0)
应该是这样吧 - -
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-5-15 00:00:42 | 显示全部楼层
Nyu菌 发表于 2019-5-14 23:36
懂了。。。 我好蠢 _(:з」∠)_
p1=point(3,5)
p2=point(0,0)

      没错!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-1-15 20:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表