擅长:python、mysql、java
<p>地精应该是敌人的一个例子。但是,在您的示例中添加了一个名为<code>attack</code>的方法。我想说最好的方法是:</p>
<pre><code>class Enemy:
#Base Stats for all enemies
def __init__ (self, **kwargs):
self.name = "foo"
self.current_health = 4
self.max_health = 4
self.attack = 0
self.defense = 0
self.armor = 0
self.initiative = 0
self.initMod = 0
self.alive = True
# Use this to specify any other parameters you may need
for key in kwargs:
exec ("self.%s = %s" % (key, kwargs[key])
</code></pre>
<p>现在可以创建名为goblin的<code>Enemy</code>实例。你知道吗</p>
<p>为了回答您最初提出的何时使用子类的问题,我想说使用子类是很好的,但是在您的情况下可能会使事情过于复杂。由于您的子类中只有一个方法,您可以轻松地创建一个模块,然后在<code>__init__</code>中有一个参数来指定<code>attack</code>函数。这将更简化您的代码。你知道吗</p>
<blockquote>
<p>Note: If you are going to use subclasses make sure that in the <code>__init__</code> of your subclass you call the <code>__init__</code> of your baseclass with <code>super()</code>.</p>
</blockquote>