定义抽象基类(Abstract Base Class)
2016-10-25 00:00
477 查看
摘要: 通过代码实例介绍了抽象基类的定义方法,代码可直接运行。
#!/usr/bin/python3 import abc #这里定义了一个抽象基类(ABC),继承abc.ABC class animal(abc.ABC): @abc.abstractmethod def getName(self): """获取动物名称""" #定义一个子类 class Cat(animal): def getName(self): return '猫,Hellokitty' #定义了子类,但是没有重定义虚函数getName. #import时不会报错,运行时报错,即定义对象时报错,如下所示。 class Dog(animal): def __init__(self): print('汪汪汪...') if __name__=='__main__': c=Cat() print(isinstance(c,animal)) d=Dog()#此处报错--->TypeError: Can't instantiate abstract class Dog with abstract methods getName
相关文章推荐
- C++ - 纯虚函数(pure virtual) 和 抽象基类(abstract base class)
- django abstract base class ---- 抽象基类
- C++ - 纯虚函数(pure virtual) 和 抽象基类(abstract base class)
- Abstract Class yii\base\Request
- public abstract class CollectionBase:IList,ICollection,IEnumerable
- Abstract Class yii\base\Application
- public abstract class DockPaneStripBase : Control 关闭窗口
- Abstract Class yii\widgets\BaseListView
- Abstract Class yii\base\ErrorHandler
- abstract class BaseAction
- Python - base class and the AbstractMethod/Abst...
- Django CBV(Class-Base View) vs FBV(Function-Base View)
- RS232 Serial Comm (Use P/Invoke to Develop a .NET Base Class Library for Serial Device Communications)
- 在class中,当你重新定义method_messing的时候, respond_to?也需要重新定义
- Parser Error Message: The base class includes the fieldthe type of control (ASP.
- class std::basic_string 找到一个或多个多重定义的符号
- Javascript定义类(class)的三种方法
- org.apache.catalina.loader.WebappClassLoaderBase.checkStateForResourceLoading Illegal access
- C#定义父类PageBase检查登陆及消息提示
- The difference between Asp.Webpart and WSS.Webpart baseclass