繁体   English   中英

在子类python中调用基类方法

[英]calling a base class method in the child class python

到底是怎么回事。 我已经查看了其他有关堆栈溢出的解决方案,但是从我所看到的来看,它们似乎都不起作用。 我有一个具有更改基本属性值的方法的基础对象。 当我在子类(继承)中调用基函数时,我得到子类没有属性“ 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())

编辑:道歉,我本来从未自我过。

简单-省略第二个自我:

self.addComponent(AComponentObject())

您看,以上内容实际上翻译为

addComponent(self, AComponentObject())

换句话说:本质上,“ OO”适用于具有隐式 this / self指针(无论您如何命名)作为参数的函数。

您为.addComponent()方法使用了不正确的参数。

# ...

class Circle(GameObject):

 def __init__(self):
     super(GameObject,self).__init__()
     # NOT A PROBLEM STATEMENT ANYMORE
     self.addComponent(AComponentObject())
     # ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM