[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
什么是接口 ?
接口只是定义了一些方法,而没有去实现,多用于程序设计时,只是设计需要有什么样的功能,但是并没有实现任何功能,这些功能需要被另一个类(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()
*