鱼C论坛

 找回密码
 立即注册
查看: 1926|回复: 2

[已解决]第38讲的一个问题求助

[复制链接]
发表于 2017-6-2 21:35:14 | 显示全部楼层 |阅读模式

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

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

x
题目是:定义一个点类和一个直线类,使用getLen方法获得线段长度

  1. import random as r
  2. import math as m
  3. class Point:
  4.     def __init__(self):
  5.         self.x = r.randint(1,10)
  6.         self.y = r.randint(1,10)
  7.         
  8. class Line(Point):

  9.     point1 = Point()
  10.     point2 = Point()
  11.     def getLen(self):
  12.         return m.sqrt((self.point1.x-self.point2.x)**2+(self.point1.y-self.point2.y)**2)

  13. y = []
  14. for i in range(10):
  15.     temp = Line()
  16.     y.append(temp)
  17.     print('y[%d]=%f' %(i,y[i].getLen()))
复制代码


返回的结果为:
  1. y[0]=5.830952
  2. y[1]=5.830952
  3. y[2]=5.830952
  4. y[3]=5.830952
  5. y[4]=5.830952
  6. y[5]=5.830952
  7. y[6]=5.830952
  8. y[7]=5.830952
  9. y[8]=5.830952
  10. y[9]=5.830952
复制代码


为什么每一次算的结果都是一样的呢?

谢谢各位!
最佳答案
2017-6-2 22:13:51
把8~11行改为:
  1. class Line():
  2.     def __init__(self):
  3.         self.point1 = Point()
  4.         self.point2 = Point()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-6-2 22:13:51 | 显示全部楼层    本楼为最佳答案   
把8~11行改为:
  1. class Line():
  2.     def __init__(self):
  3.         self.point1 = Point()
  4.         self.point2 = Point()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-6-22 16:03:17 | 显示全部楼层

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-27 20:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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