python接口的实现

接口:
-url
-数据类型,python不存在
class 类名
1、类中的方法可以写任意多个
2、如果想要对类中的方法做约束,就需要写接口
接口中定义一个方法f1,可以约束继承他的子类
class 接口:
   def f1(self):
    pass
class 类名(接口):
  pass
示例:
class IOrderRepository:(一般接口类默认为I大写开头)

  def fetch_one_by(self,nid):
    """
    所有继承当前类的类必须实现该方法
    """
    raise Execption('子类中必须实现该方法')
class OrderRepository(IOrderRepository):#继承接口
  def fetch_one_by(self,nid):
    print(123123)
#由于python中无接口类型,所以需要人为的构造出来(在父类接口类中增加异常触发raise Execption('子类中必须实现该方法'))

猜你喜欢

转载自www.cnblogs.com/qiangayz/p/9352159.html