擅长:python、mysql、java
<p>如果正确实现继承图,可以使用<code>super</code>实现。为了让它工作,<code>Human</code>和<code>Robot</code>都需要一个可以<code>Talk</code>的公共基类。你知道吗</p>
<pre><code>class Thing:
def Talk(self):
# Things don't talk, but some more complex things may
return ''
class Robot(Thing):
def Talk(self):
return 'I am a computer!\n' + super().Talk()
class Human(Thing):
def Talk(self):
return 'I am an organic being!\n' + super().Talk()
class Cyborg(Human, Robot):
def Talk(self):
return super().Talk()
</code></pre>
<p>这是一个谈话的例子。你知道吗</p>
<pre><code>>>> Cyborg().Talk()
I am an organic being!
I am a computer!
>>> Robot().Talk()
I am a computer!
>>> Human().Talk()
I am an organic being!
</code></pre>