<p>抽象基类已经做了您想要的事情。<code>abstractmethod</code>与让您使用<code>super</code>调用方法无关;无论如何,您都可以这样做。相反,必须重写用<code>abstractmethod</code>修饰的任何方法,才能使子类可实例化:</p>
<h3>Python3:</h3>
<pre><code>>>> class Foo(metaclass=abc.ABCMeta):
... @abc.abstractmethod
... def foo(self):
... pass
...
>>> class Bar(Foo):
... pass
...
>>> class Baz(Bar):
... def foo(self):
... return super(Baz, self).foo()
...
>>> Foo()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Can't instantiate abstract class Foo with abstract methods foo
>>> Bar()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Can't instantiate abstract class Bar with abstract methods foo
>>> Baz()
<__main__.Baz object at 0x00000210D702E2B0>
</code></pre>
<h3>Python2:</h3>
<pre><code>>>> class Foo(object):
... __metaclass__ = abc.ABCMeta
... @abc.abstractmethod
... def foo(self): pass
...
>>> class Bar(Foo): pass
...
>>> class Baz(Bar):
... def foo(self): return super(Baz, self).foo()
...
>>> Foo()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Can't instantiate abstract class Foo with abstract methods foo
>>> Bar()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Can't instantiate abstract class Bar with abstract methods foo
>>> Baz()
<__main__.Baz object at 0x0000000001EC10B8>
</code></pre>