|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import random as r
import math
class Point():
def a(self):
self.point_a_x=random.randint(0,10)
self.point_a_y=random.randint(0,10)
def b(self):
self.point_b_x=random.randint(0,10)
self.point_b_y=random.randint(0,10)
class GetLen(Point):
def Get(self):
self.z=math.sqrt((self.point_a_x-self.point_b_x)**2+(self.point_a_y-self.point_b_y)**2)
def re_z(self):
return self.z
请问大佬们,这行代码有什么问题,我是按照自己的想法打出来的,是实现在坐标轴里面X和Y的直线距离,然后错误一直都是
Traceback (most recent call last):
File "<pyshell#28>", line 1, in <module>
b.re_z()
File "C:/Users/Administrator/Desktop/2.py", line 20, in re_z
return self.z
AttributeError: 'GetLen' object has no attribute 'z'
求解答,如果可以的话大佬们应该可以从我的代码中发现我有什么不足的地方,望指教!!!
- import random #你的这个地方有问题,改成这样,不然后续无法使用
- import math
- class Point():
- #这里是调用2个函数,目的是产生类的基本属性,方便继承类的调用
- def __init__(self):
- self.a()
- self.b()
- def a(self):
- self.point_a_x=random.randint(0,10)
- self.point_a_y=random.randint(0,10)
- def b(self):
- self.point_b_x=random.randint(0,10)
- self.point_b_y=random.randint(0,10)
- class GetLen(Point):
- def Get(self):
- self.z=math.sqrt((self.point_a_x-self.point_b_x)**2+(self.point_a_y-self.point_b_y)**2)
-
- def re_z(self):
- return self.z
-
- c = GetLen()
- c.Get() #先必须调用这个函数,不然z是不存在的
- test = c.re_z()
- print(test)
复制代码
类是可以向下继承的,那你的这个例子就是继承类,参数已经传递到子类了
至于你说的 互相传递,难道你要把子类的参数传递到父类?这个做法并不是不能,只是,你可能需要好好看看类的继承方面的知识点
上面是我在你的代码基础上改的的
|
|