|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import math as m
class Point:
def __init__(self,x,y):
self.x = x
self.y = y
def print_x(self):
return self.x
def print_y(self):
return self.y
class Line:
def getLen(self,x1,x2,y1,y2):
long = m.sqrt((self.x1-self.x2)**2 + (self.y1-self.y2)**2)
print(long)
one = Point(6,3)
two = Point(8,4)
point_1 = one.print_x
point_2 = one.print_y
point_3 = two.print_x
point_4 = two.print_y
count = Line()
count.getLen(point_1,point_3,point_2,point_4)
为什么会出现'Line' object has no attribute 'x1'错误,代码要怎么改,求大神支招
- import math as m
- class Point:
- def __init__(self,x,y):
- self.x = x
- self.y = y
-
- def print_x(self):
- return self.x
-
- def print_y(self):
- return self.y
-
- class Line:
- def getLen(self,x1,x2,y1,y2):
- long = m.sqrt((x1-x2)**2 + (y1-y2)**2)
- print(long)
- one = Point(6,3)
- two = Point(8,4)
- point_1 = one.print_x() # 加() 返回值
- point_2 = one.print_y() # 加() 返回值
- point_3 = two.print_x() # 加() 返回值
- point_4 = two.print_y() # 加() 返回值
- count = Line()
- count.getLen(point_1,point_3,point_2,point_4)
- '''
- # 或者
- class Line:
- def __init__(self, x1, x2, y1, y2): # 初始化是在 __init__ 魔法方法里
- self.x1 = x1
- self.x2 = x2
- self.y1 = y1
- self.y2 = y2
-
-
- def getLen(self):
- long = m.sqrt((self.x1-self.x2)**2 + (self.y1-self.y2)**2)
- print(long)
- one = Point(6,3)
- two = Point(8,4)
- point_1 = one.print_x() # 加() 返回值
- point_2 = one.print_y() # 加() 返回值
- point_3 = two.print_x() # 加() 返回值
- point_4 = two.print_y() # 加() 返回值
- count = Line(point_1,point_3,point_2,point_4)
- count.getLen()
-
- '''
复制代码
|
|