[英]Python 3 class inheritance with parameters
所以我有一个类,字符()和子类,npc(字符)。 它们看起来像这样:
class character():
def __init__(self,name,desc):
self.name = name
self.desc = desc
self.attr = ""
#large list of attributes not defined by parameters
和
class npc(character):
def __init__(self,greetings,topics):
self.greetings = greetings
self.topics = topics
character.__init__(self)
self.pockets = []
#more attributes specific to the npc subclass not defined by parameters
然而,当我从'Npc'中调用'Character'中应该存在(或者我认为)的属性时,比如'name'或'desc'或'attr',我得到一个“不存在/未定义”的错误。 我只是不做继承吗? 这里发生了什么? 我混淆了属性和参数吗?
你的类角色的构造函数是:
class character():
def __init__(self, name, desc):
所以你必须准确的名字和desc当你使npc herited。 因为我个人更喜欢超级,这将是:
class npc(character):
def __init__(self,greetings,topics):
super().__init__("a_name", "a_desc")
self.greetings = greetings
self.topics = topics
self.pockets = []
#more attributes specific to the npc subclass not defined by parameters
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.