过去一周我一直在玩Python,并遇到将4个参数传递给类方法的问题。将参数传递给Python中的类方法的问题
class Line:
locx0 = 0
locy0 = 0
locx1 = 0
locy1 = 0
def __init__(self):
print'<<Line __init__()>>'
def setLineCoordinates(locx0, locy0, locx1, locy1):
self.locx0 = locx0
self.locy0 = locy0
self.locx1 = locx1
self.locy1 = locy1
def getLineCoordinatesX0():
return self.x0
def getLineCoordinatesY0():
return self.y0
def getLineCoordinatesX1():
return self.x1
def getLineCoordinatesY0():
return self.y0
这里就是我称之为类方法:
def LineDepot():
x0 = None
x1 = None
y0 = None
y1 = None
line = Line()
print"Please enter starting and ending coordinates "
print"If no value is entered, then it will be assumed that the coordinate value is zero "
x0 = int(input('Enter value for initial x coordiante : '))
y0 = int(input('Enter value for initial y coordiante : '))
x1 = int(input('Enter value for end x coordiante :'))
y1 = int(input('Enter value for end y coordiante :'))
line.setLineCoordinates(x0, y0, x1, y1)
这是我一直得到错误
下面是它的类中定义的类方法在输出中:
Please make a selection from the following menu...
1.Create a new Line
2.View current lines
3.View logs
4.Mail line info or logs
5.View summary of line stats
6.Exit this program
Menu Selection:1
<<Line __init__()>>
Please enter starting and ending coordinates
If no value is entered, then it will be assumed that the coordinate value is zero
Enter value for initial x coordiante : 1
Enter value for initial y coordiante : 2
Enter value for end x coordiante :3
Enter value for end y coordiante :4
Traceback (most recent call last):
File "./linear.line.py", line 107, in <module>
Main()
File "./linear.line.py", line 15, in Main
Menu()
File "./linear.line.py", line 52, in Menu
LineDepot()
File "./linear.line.py", line 32, in LineDepot
line.setLineCoordinates(x0, y0, x1, y1)
TypeError: setLineCoordinates() takes exactly 4 arguments (5 given)
我试图找出我的生活为什么当我通过4个参数,解释是 告诉我,我试图通过5
我已经和继续在研究的过程中问题。
任何帮助,这将不胜感激。 谢谢!
格式注:请缩进代码段和等4位。谢谢! – 2010-08-09 19:10:08
确保让你的类从'object'继承。如果你不这样做,你会使用旧式的课程,那很糟糕。 – Daenyth 2010-08-09 19:14:43
另一件需要注意的是:这些函数是实例方法,而不是类方法。 – Daenyth 2010-08-09 19:15:41