如何让一个摘要 Class 从 Python 中的另一个摘要 Class 继承?
How to make an Abstract Class inherit from another Abstract Class in Python?
是否可以从 Python 中的另一个摘要 Class 继承一个摘要 Class?
如果是这样,我应该怎么做?
查看 abc
模块。对于 2.7:link. For 3.6: link
简单的例子给你:
from abc import ABC, abstractmethod
class A(ABC):
def __init__(self, value):
self.value = value
super().__init__()
@abstractmethod
def do_something(self):
pass
class B(A):
@abstractmethod
def do_something_else(self):
pass
class C(B):
def do_something(self):
pass
def do_something_else(self):
pass
from abc import ABCMeta, abstractmethod
class OriginalAbstractclass(metaclass=ABCMeta):
@abstractmethod
def sample_method(self):
pass
class InheritedAbstractClass(OriginalAbstractclass, metaclass=ABCMeta):
@abstractmethod
def another_method(self):
pass
class ConcreteClass(InheritedAbstractClass):
def some_method(self):
pass
def another_method(self):
pass
是否可以从 Python 中的另一个摘要 Class 继承一个摘要 Class?
如果是这样,我应该怎么做?
查看 abc
模块。对于 2.7:link. For 3.6: link
简单的例子给你:
from abc import ABC, abstractmethod
class A(ABC):
def __init__(self, value):
self.value = value
super().__init__()
@abstractmethod
def do_something(self):
pass
class B(A):
@abstractmethod
def do_something_else(self):
pass
class C(B):
def do_something(self):
pass
def do_something_else(self):
pass
from abc import ABCMeta, abstractmethod
class OriginalAbstractclass(metaclass=ABCMeta):
@abstractmethod
def sample_method(self):
pass
class InheritedAbstractClass(OriginalAbstractclass, metaclass=ABCMeta):
@abstractmethod
def another_method(self):
pass
class ConcreteClass(InheritedAbstractClass):
def some_method(self):
pass
def another_method(self):
pass