标签:最简 基类 imp fun rac cme pytho jsb bst
在Python3.4中,声明抽象基类最简单的方式是子类话abc.ABC;Python3.0到Python3.3,必须在class语句中使用metaclass=ABCMeta;Python2中使用__metaclass__=ABCMeta
Python3.4 实现方法:
from abc import ABC, abstractmethod class Foo(ABC): @abstractmethod def fun(self): ‘‘‘please Implemente in subclass‘‘‘ class SubFoo(Foo): def fun(self): print(‘fun in SubFoo‘) a = SubFoo() a.fun()
Python3.0到Python3.3的实现方法:
from abc import abstractmethod, ABCMeta class Bar(metaclass=ABCMeta): @abstractmethod def fun(self): ‘‘‘please Implemente in subclass‘‘‘ class SubBar(Bar): def fun(self): print(‘fun in SubBar‘) b = SubBar() b.fun()
Python2的实现方法:
from abc import ABCMeta, abstractmethod class FooBar(): __metaclass__ = ABCMeta @abstractmethod def fun(self): ‘‘‘please Implemente in subclass‘‘‘ class SubFooBar(FooBar): def fun(self): print(‘fun in SubFooBar‘) a = SubFooBar() a.fun()
Python2和Python3中@abstractmethod的用法
标签:最简 基类 imp fun rac cme pytho jsb bst
原文地址:https://www.cnblogs.com/qiumingcheng/p/12244896.html