码迷,mamicode.com
首页 > 编程语言 > 详细

python 用abc模块构建抽象基类Abstract Base Classes

时间:2018-08-01 18:55:53      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:nbsp   name   exce   utf-8   usr   ESS   import   sage   cme   

  见代码:

 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

标签:nbsp   name   exce   utf-8   usr   ESS   import   sage   cme   

原文地址:https://www.cnblogs.com/tlz888/p/9403256.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!