发生了什么事。我已经看过堆栈溢出的其他解决方案,但似乎没有从我看到的工作。我有一个基础对象,其方法可以更改基本属性的值。当我在子类(继承)中调用基函数时,我得到子类没有属性“baseAttribute”
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())
编辑: 道歉,我从来没有过自我。
答案 0 :(得分:4)
简单 - 省略第二个自我:
self.addComponent(AComponentObject())
你看,上面实际上转换为
addComponent(self, AComponentObject())
换句话说:本质上“OO”适用于具有隐式 此 / 自我指针的函数(但是您可以将其命名为)作为论点。
答案 1 :(得分:0)
您使用.addComponent()
方法的错误参数。
# ...
class Circle(GameObject):
def __init__(self):
super(GameObject,self).__init__()
# NOT A PROBLEM STATEMENT ANYMORE
self.addComponent(AComponentObject())
# ...