|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
题目不方便贴过来 不过主要是要创建一个点类和一个直线类,通过getLen函数求得两点之间直线长度
我的代码是这样的:
- import math as m
- class Point:
- def __init__(self, x, y):
- self.x = x
- self.y = y
- class Line:
- def getpoint(self,x1,y1,x2,y2):
- self.x1 = getattr(point1,x)
- self.x2 = getattr(point2,x)
- self.y1 = getattr(point1,y)
- self.y2 = getattr(point2,y)
- def getLen(self):
- print(m.sqrt((self.x1 - self.x2)**2 + (self.y1 - self.y2)**2))
- point1 = Point(1,5)
- point2 = Point(3,6)
- line = Line()
- line.getpoint()
- line.getLen()
复制代码
系统提示错误:
- Traceback (most recent call last):
- File "/Users/williamwang/PycharmProjects/类和对象/venv/test1.py", line 32, in <module>
- line.getpoint()
- TypeError: getpoint() missing 4 required positional arguments: 'x1', 'y1', 'x2', and 'y2'
复制代码
请问大神错在哪里~~~该怎么修改才行
修改的:
- import math as m
- class Point:
- def __init__(self, x, y):
- self.x = x
- self.y = y
- class Line:
- def getpoint(self):
- self.x1 = getattr(point1,'x')
- self.x2 = getattr(point2,'x')
- self.y1 = getattr(point1,'y')
- self.y2 = getattr(point2,'y')
- def getLen(self):
- print(m.sqrt((self.x1 - self.x2)**2 + (self.y1 - self.y2)**2))
- point1 = Point(1,5)
- point2 = Point(3,6)
- line = Line()
- line.getpoint()
- line.getLen()
复制代码
|
|