Python标准库--abc模块

abc--抽象基类

注册一个具体类

class PluginBase(metaclass=abc.ABCMeta):
    @abc.abstractmethod
    def load(self, input):
        pass

    @abc.abstractmethod
    def save(self, output, data):
        pass

class LocalBaseClass(object):
    pass

class RegisteredImplementation(LocalBaseClass):
    def load(self, input):
        return input.read()

    # def save(self, output, data):
    #     return output.write(data)
# 注册,可以不必实现所有抽象方法
PluginBase.register(RegisteredImplementation)

print(issubclass(RegisteredImplementation, PluginBase))
print(isinstance(RegisteredImplementation(), PluginBase))

继承实现

class SubclassImpementation(PluginBase):

    def load(self, input):
        return input.read()

    def save(self, output, data):
        return output.write(data)

# 继承,必须实现所有抽象方法
print(issubclass(SubclassImpementation, PluginBase))
print(isinstance(SubclassImpementation(), PluginBase))

读写属性

class Implementation():
    _value = 'Default value'

    def value_getter(self):
        return self._value

    def value_setter(self, newvalue):
        self._value = newvalue

    value = property(value_getter, value_setter)
    
    # @property
    # def value(self):
    #     return self._value
posted @ 2017-06-18 21:59  兔头咖啡  阅读(1516)  评论(0编辑  收藏  举报


作者:兔头咖啡
出处:http://www.cnblogs.com/wj5633/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。