Python类的约束

Python类的约束

class BasePlugin(object):
    #基类,用于做约束。约束子类中必须实现process方法
    def process(self):
        raise NotImplementedError('%s中必须实现process方法' % self.__class__.__name__)


class Ab(BasePlugin):
    def __init__(self):
        pass

class Ac(BasePlugin):
    def __init__(self):
        pass

a = Ab()
a.process()


#如果不写process方法,会返回错误信息
E:\PYTHON\autoclient\venv\Scripts\python.exe E:/PYTHON/autoclient/lib/plugins/base.py
Traceback (most recent call last):
  File "E:/PYTHON/autoclient/lib/plugins/base.py", line 28, in <module>
    a.process()
  File "E:/PYTHON/autoclient/lib/plugins/base.py", line 16, in process
    raise NotImplementedError('%s中必须实现process方法' % self.__class__.__name__)
NotImplementedError: Ab中必须实现process方法
posted @ 2020-07-08 22:37  国际一级退堂鼓鼓手  阅读(108)  评论(0编辑  收藏  举报