- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
关于 SO 的类似问题包括:this one和 this .我也通读了我能找到的所有在线文档,但我仍然很困惑。如果你能提供帮助,我将不胜感激。
我想在我的 CastSpell 类 lumus 方法中使用 Wand 类的 .wandtype 属性。但我不断收到错误消息“AttributeError:‘CastSpell’对象没有属性‘wandtype’。”
此代码有效:
class Wand(object):
def __init__(self, wandtype, length):
self.length = length
self.wandtype = wandtype
def fulldesc(self):
print "This is a %s wand and it is a %s long" % (self.wandtype, self.length)
class CastSpell(object):
def __init__(self, spell, thing):
self.spell = spell
self.thing = thing
def lumus(self):
print "You cast the spell %s with your wand at %s" %(self.spell, self.thing)
def wingardium_leviosa(self):
print "You cast the levitation spell."
my_wand = Wand('Phoenix-feather', '12 inches')
cast_spell = CastSpell('lumus', 'door')
my_wand.fulldesc()
cast_spell.lumus()
此代码尝试继承,但没有。
class Wand(object):
def __init__(self, wandtype, length):
self.length = length
self.wandtype = wandtype
def fulldesc(self):
print "This is a %s wand and it is a %s long" % (self.wandtype, self.length)
class CastSpell(Wand):
def __init__(self, spell, thing):
self.spell = spell
self.thing = thing
def lumus(self):
print "You cast the spell %s with your %s wand at %s" %(self.spell, self.wandtype, self.thing) #This line causes the AttributeError!
print "The room lights up."
def wingardium_leviosa(self):
print "You cast the levitation spell."
my_wand = Wand('Phoenix-feather', '12 inches')
cast_spell = CastSpell('lumus', 'door')
my_wand.fulldesc()
cast_spell.lumus()
我试过使用 super() 方法无济于事。非常感谢您帮助理解 a) 为什么类继承在这种情况下不起作用,b) 如何让它起作用。
最佳答案
简单来说,你在继承自它的类中覆盖了 Wand.__init__
,所以 CastSpell.wandtype
永远不会在 CastSpell
中设置>。除此之外,my_wand
无法将信息传递给 cast_spell
,因此您对继承的作用感到困惑。
无论您如何操作,您都必须以某种方式将length
和wandtype
传递给CastSpell
。一种方法是将它们直接包含到 CastSpell.__init__
中:
class CastSpell(Wand):
def __init__(self, spell, thing, length, wandtype):
self.spell = spell
self.thing = thing
self.length = length
self.wandtype = wandtype
另一种更通用的方法是将这两个传递给基类自己的 __init__()
:
class CastSpell(Wand):
def __init__(self, spell, thing, length, wandtype):
self.spell = spell
self.thing = thing
super(CastSpell, self).__init__(length, wandtype)
另一种方法是停止让 CastSpell
从 Wand
继承(CastSpell
是一种 Wand
吗?或者 Wand
做的事情?),而是让每个 Wand 都能够在其中包含一些 CastSpell
:而不是“is-a”(a CastSpell
是一种 Wand
),尝试“has-a”(Wand
有 Spell
)。
这里有一个简单但不是很好的方法来使用魔杖存储法术:
class Wand(object):
def __init__(self, wandtype, length):
self.length = length
self.wandtype = wandtype
self.spells = {} # Our container for spells.
# You can add directly too: my_wand.spells['accio'] = Spell("aguamenti", "fire")
def fulldesc(self):
print "This is a %s wand and it is a %s long" % (self.wandtype, self.length)
def addspell(self, spell):
self.spells[spell.name] = spell
def cast(self, spellname):
"""Check if requested spell exists, then call its "cast" method if it does."""
if spellname in self.spells: # Check existence by name
spell = self.spells[spellname] # Retrieve spell that was added before, name it "spell"
spell.cast(self.wandtype) # Call that spell's cast method, passing wandtype as argument
else:
print "This wand doesn't have the %s spell." % spellname
print "Available spells:"
print "\n".join(sorted(self.spells.keys()))
class Spell(object):
def __init__(self, name, target):
self.name = name
self.target = target
def cast(self, wandtype=""):
print "You cast the spell %s with your %s wand at %s." % (
self.name, wandtype, self.target)
if self.name == "lumus":
print "The room lights up."
elif self.name == "wingardium leviosa":
print "You cast the levitation spell.",
print "The %s starts to float!" % self.target
def __repr__(self):
return self.name
my_wand = Wand('Phoenix-feather', '12 inches')
lumus = Spell('lumus', 'door')
wingardium = Spell("wingardium leviosa", "enemy")
my_wand.fulldesc()
lumus.cast() # Not from a Wand! I.e., we're calling Spell.cast directly
print "\n\n"
my_wand.addspell(lumus) # Same as my_wand.spells["lumus"] = lumus
my_wand.addspell(wingardium)
print "\n\n"
my_wand.cast("lumus") # Same as my_wand.spells["lumus"].cast(my_wand.wandtype)
print "\n\n"
my_wand.cast("wingardium leviosa")
print "\n\n"
my_wand.cast("avada kadavra") # The check in Wand.cast fails, print spell list instead
print "\n\n"
关于Python 类继承 AttributeError - 为什么?怎么修?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10670020/
我是一名优秀的程序员,十分优秀!