一、预备知识:

1、接口:

  - URL形式  

  - 数据类型 (Python中不存在)

 

a.类中的方法可以写任意个,想要对类中的方法进行约束就可以使用接口;

b.定义一个接口,接口中定义一个方法f1;

c.只要继承(实现)了接口,那么类就会受约束,该类必须要有f1方法!

d.接口只用来做约束,不需要写具体功能。

由于python中无接口类型,但是可以人为构造,抛出异常!

raise Exception('子类中必须实现该方法')

class IOrderRepository:
    def fetch_one_by(self,nid):
        '''
        获取单条数据的方法,所有继承当前类的类必须实现(有)该方法
        :param nid:
        :return:
        '''
        raise Exception('子类中必须实现该方法')

class OrderRepository(IOrderRepository):
    
    def fetch_one_by(self,nid):
        print('...获取数据')


obj = OrderRepository
obj.fetch_one_by(1)
demo

相关文章:

  • 2021-10-19
  • 2022-01-20
  • 2021-05-06
  • 2022-02-01
  • 2021-07-05
  • 2021-08-27
  • 2021-07-21
  • 2022-01-19
猜你喜欢
  • 2021-04-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-28
  • 2021-09-07
  • 2022-01-23
  • 2021-08-10
相关资源
相似解决方案