擅长:python、mysql、java
<p>您需要接受<code>Celebrity</code>中<code>__init__()</code>方法中的<code>age</code>参数,然后将其传递给基类<code>__init__()</code>:</p>
<pre><code>class Person(object):
def __init__(self,age):
self.age = age
class Celebrity(Person):
def __init__(self, age, profession):
self.profession = profession
super(Celebrity, self).__init__(age)
morganFreeman = Celebrity(55, "actor")
bob = Person(45)
>>> bob = Person(45)
>>> morganFreeman = Celebrity(55, "actor")
>>> morganFreeman.age
55
>>> morganFreeman.profession
'actor'
>>> bob.age
45
>>> bob.profession
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Person' object has no attribute 'profession'
</code></pre>
<p>这表明<code>Celebrity</code>对象同时具有<code>age</code>和<code>profession</code>属性,而<code>Person</code>属性仅具有<code>age</code>。你知道吗</p>