擅长:python、mysql、java
<p>您正在重写父类的构造函数(<code>__init__</code>)。要扩展它,需要用<a href="http://docs.python.org/library/functions.html#super" rel="nofollow">^{<cd2>}</a>调用显式调用父级的构造函数。</p>
<pre><code>class TypeTwoEvent(Event):
def __init__(self, level=None, **kwargs):
# the super call to set the attributes in the parent class
super(TypeTwoEvent, self).__init__(**kwargs)
# now, extend other attributes
self.sr1 = level
self.state = STATE_EVENT_TWO
</code></pre>
<p>请注意,<code>super</code>调用不是始终位于子类中<code>__init__</code>方法的顶部。它的位置取决于你的情况和逻辑。</p>