2017-02-07 6 views
1

何が起こっているのですか?私はスタックオーバーフローで他のソリューションを見てきましたが、私が見たものではうまくいかないようです。私は基本属性の値を変更するメソッドを持つ基本オブジェクトを持っています。 謝罪、私はもともと自己に渡されたことがない:私は子クラス(継承)で基本関数を呼び出すとき、私は子供のクラスは、属性「baseAttribute」子クラスのPythonで基本クラスメソッドを呼び出す

class GameObject(object): 
#This is the base class for gameObjects 
def __init__(self): 
    self.components = {} 

def addComponent(self, comp): 
    self.components[0] = comp #ignore the index. Placed 0 just for illustration 

class Circle(GameObject): 
#circle game object 
def __init__(self): 
    super(GameObject,self).__init__() 
    #PROBLEM STATEMENT 
    self.addComponent(AComponentObject()) 
    #or super(GameObject,self).addComponent(self,AComponentObject()) 
    #or GameObject.addComponent(self, AComponentObject()) 

EDITを持っていないことを取得します。

+0

私は驚くべきことを認めています;-) – GhostCat

答えて

4

シンプル - 第二の自己除外:あなたが見

self.addComponent(AComponentObject()) 

を、上記のは、実際に言い換える

addComponent(self, AComponentObject()) 

に変換します。本質的に「OOは」を持っている機能で動作します暗黙的なこの/の自己ポインタ(ただし名前を付けてください)を引数とします。

0

.addComponent()メソッドの引数が正しくありません。

# ... 

class Circle(GameObject): 

def __init__(self): 
    super(GameObject,self).__init__() 
    # NOT A PROBLEM STATEMENT ANYMORE 
    self.addComponent(AComponentObject()) 
    # ... 
関連する問題