過去一週我一直在玩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