|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 supercyt 于 2016-12-29 20:26 编辑
- import random
- import math
- #定义点类
- class Point():
- def __init__(self):
- self.x1 = random.random()
- self.y1 = random.random()
- self.x2 = random.random()
- self.y2 = random.random()
- #定义直线类
- class Line(Point):
- def _init_(self):
- super().__init__()
- self.distance = math.sqrt((self.x1-self.x2)**2+(self.y1-self.y2)**2)
- a= Line()
- print a.x1,a.x2,a.y1,a.y2
- print a.distance
复制代码
显示结果:
0.889645228951 0.618441493572 0.122180810388 0.363903186572
Traceback (most recent call last):
File "D:\fishc\class_test.py", line 52, in <module>
AttributeError: Line instance has no attribute 'distance'
a.distance 这个属性不存在? 为什么? x1,x2,y1,y2都继承了 为什么不能用到计算里? Python 2.7 |
|