擅长:python、mysql、java
<p>如果您不想修改原始的类定义,那么您可以通过简单的继承本身而不是使用装饰器来实现这一点</p>
<pre><code>class Test(object):
def __init__(self):
pass
def bar(self, x):
print("In base bar..")
return x
class DerivedTest(Test):
def __init__(self):
super().__init__()
def bar(self,x):
print("In derive's bar..")
super().bar(x)
</code></pre>
<p>现在,在执行时说:</p>
<pre><code>dt=DerivedTest()
dt.bar(10)
</code></pre>
<p>输出将是</p>
<pre><code>In derive's bar..
In base bar..
</code></pre>
<p>您可以在super()调用之前和之后放置您想要的任何包装器代码</p>