擅长:python、mysql、java
<p>只需将参数作为关键字参数传递:</p>
<pre><code>class Pet(object):
def __init__(self, **kwargs):
if not "n" in kwargs:
raise ValueError("Please specify the number I am to print")
print kwargs["n"]
class Dog(Pet):
def __init__(self, **kwargs):
Pet.__init__(self, **kwargs)
</code></pre>
<p>但是,您应该使用<code>super</code>,而不是硬编码超类。在</p>
<p>将<code>Dog</code>的定义更改为(Python 2.X):</p>
^{pr2}$
<p>在Python 3.X中,它更好,只是:</p>
<pre><code>class Dog(Pet):
def __init__(self, **kwargs):
super().__init__(**kwargs)
</code></pre>