栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python中的接口

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python中的接口

什么是接口 ?

接口只是定义了一些方法,而没有去实现,多用于程序设计时,只是设计需要有什么样的功能,但是并没有实现任何功能,这些功能需要被另一个类(B)继承后,由 类B去实现其中的某个功能或全部功能。

在python中接口由抽象类和抽象方法去实现,接口是不能被实例化的,只能被别的类继承去实现相应的功能。

个人觉得接口在python中并没有那么重要,因为如果要继承接口,需要把其中的每个方法全部实现,否则会报编译错误,还不如直接定义一个class,其中的方法实现全部为pass,让子类重写这些函数。

当然如果有强制要求,必须所有的实现类都必须按照接口中的定义写的话,就必须要用接口。

方法一:用抽象类和抽象函数实现方法
#抽象类加抽象方法就等于面向对象编程中的接口
from abc import ABCMeta,abstractmethod

class interface(object):
    __metaclass__ = ABCMeta #指定这是一个抽象类
    @abstractmethod  #抽象方法
    def Lee(self):
        pass
    
    def Marlon(self):
        pass


class RelalizeInterfaceLee(interface):#必须实现interface中的所有函数,否则会编译错误
    def __init__(self):    
        print '这是接口interface的实现'
    def Lee(self):
        print '实现Lee功能'        
    def Marlon(self):
        pass   
 

class RelalizeInterfaceMarlon(interface): #必须实现interface中的所有函数,否则会编译错误
    def __init__(self):    
        print '这是接口interface的实现'
    def Lee(self):
        pass      
    def Marlon(self):
        print "实现Marlon功能"
 

方法二:用普通类定义接口

class interface(object): #假设这就是一个接口,接口名可以随意定义,所有的子类不需要实现在这个类中的函数
    def Lee(self):,
        pass
    
    def Marlon(self):
        pass
 
class Realaize_interface(interface):
    def __init__(self):
        pass
    def Lee(self):
        print "实现接口中的Lee函数"
        
        
class Realaize_interface2(interface):
    def __init__(self):
        pass
    def Marlon(self):
        print "实现接口中的Marlon函数"
     
obj=Realaize_interface()
obj.Lee()


obj=Realaize_interface2()
obj.Marlon()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/834030.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号