python 用abc模块构建抽象基类Abstract Base Classes
Posted tlz888
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 用abc模块构建抽象基类Abstract Base Classes相关的知识,希望对你有一定的参考价值。
见代码:
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 # @Time : 2018/08/01 16:58 4 from abc import ABCMeta, abstractmethod 5 6 7 class SClass(object): 8 __metaclass__ = ABCMeta 9 10 @abstractmethod 11 def my_print(self): 12 pass 13 14 15 class C0Class(SClass): 16 pass 17 18 19 class C1Class(SClass): 20 21 def my_print(self): 22 print ‘call my_print in child instance.‘ 23 24 25 if __name__ == ‘__main__‘: 26 try: 27 c0 = C0Class() 28 c0.my_print() 29 except Exception, e: 30 print e.message 31 32 c1 = C1Class() 33 c1.my_print()
输出:
1 Can‘t instantiate abstract class C0Class with abstract methods my_print 2 call my_print in child instance.
以上是关于python 用abc模块构建抽象基类Abstract Base Classes的主要内容,如果未能解决你的问题,请参考以下文章