今天爱分享给大家带来Python抽象类和接口有什么区别【面试题详解】,希望能够帮助到大家。
看看下面这个:
class Abstract1(object):
"""Some description that tells you it's abstract,
often listing the methods you're expected to supply."""
def aMethod(self):
raise NotImplementedError( "Should have implemented this" )
因为在Python中没有(也不需要)一个正式的接口协议,类Java的抽象类和接口的区别并不存在。如果有人尝试定义一个正式的接口,它其实也是一个抽象类。唯一的不同就是在文档注释的表述。
并且当你使用鸭子类型时,抽象类和接口的区别有点吹毛求疵了。
Java使用接口是因为它没有多重继承。
因为Python有多重继承,你可能还会看到类似这样的东西:
class SomeAbstraction( object ):
pass # lots of stuff - but missing something
class Mixin1( object ):
def something( self ):
pass # one implementation
class Mixin2( object ):
def something( self ):
pass # another
class Concrete1( SomeAbstraction, Mixin1 ):
pass
class Concrete2( SomeAbstraction, Mixin2 ):
pass
这是一种使用混合抽象超类去创建不相交的具体子类的方法。
原文链接:http://blog.itblood.com/4526.html,转载请注明出处。

![荒诞的诱惑0.04.1官中汉化版[PC+安卓/2.60G/更新] [国产SLG/首发/沙盒]](/wp-content/uploads/replace/2025/07/19/d0e56c3856eb9e5223b0b1802c49976d.webp)