2013-10-18 29 views
2

我试图从一个继承中移动所有类。 我写了这个小脚本:必须使用实例作为第一个参数调用unbound方法(取而代之)

class c1: 
    def move(): 
     x+=1 
     y+=1 
class c2(c1): 
    y=1 
    x=2 
c=c2 
c.move() 
print(str(c.x)+" , "+str(c.y)) 

当我运行它,我得到:

Traceback (most recent call last): File "/home/tor/Workspace/try.py", line 9, in <module> 
    c.move() TypeError: unbound method move() must be called with c2 instance as first argument (got nothing instead) [Finished in 0.1s 
with exit code 1] 

我做了什么错?

+1

您没有实例化任何类。 –

+4

你的意思是做'c = c2()'和'c.move()'吗? –

+3

您需要先了解Classes:http://docs.python.org/2/tutorial/classes.html –

回答

9
  1. 你不要实例什么

  2. 所有的方法都必须至少需要一个参数,传统上被称为self

  3. 您需要self才能访问对象字段。您的代码现在修改了该范围中不存在的局部变量。

相关问题