|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
题目是:定义一个点类和一个直线类,使用getLen方法获得线段长度
- import random as r
- import math as m
- class Point:
- def __init__(self):
- self.x = r.randint(1,10)
- self.y = r.randint(1,10)
-
- class Line(Point):
- point1 = Point()
- point2 = Point()
- def getLen(self):
- return m.sqrt((self.point1.x-self.point2.x)**2+(self.point1.y-self.point2.y)**2)
- y = []
- for i in range(10):
- temp = Line()
- y.append(temp)
- print('y[%d]=%f' %(i,y[i].getLen()))
复制代码
返回的结果为:
- y[0]=5.830952
- y[1]=5.830952
- y[2]=5.830952
- y[3]=5.830952
- y[4]=5.830952
- y[5]=5.830952
- y[6]=5.830952
- y[7]=5.830952
- y[8]=5.830952
- y[9]=5.830952
复制代码
为什么每一次算的结果都是一样的呢?
谢谢各位!
把8~11行改为:
- class Line():
- def __init__(self):
- self.point1 = Point()
- self.point2 = Point()
复制代码
|
|