python:抽象超类的实现
在python中,如何去实现一个抽象超类?看如下代码,实现了一个简单的超类模型。#!/usr/bin/env python# -*- coding: utf-8 -*-# @author : cat# @date: 2017/6/21.class SuperClass:def method(self):print("method in suer class
·
在python
中,如何去实现一个抽象超类?
看如下代码,实现了一个简单的超类模型。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @author : cat
# @date : 2017/6/21.
class SuperClass:
def method(self):
print("method in super class.")
def delegate(self):
self.action()
def action(self):
assert False, 'sub must implement this action'
class Inheritor(SuperClass):
pass
class Replacer(SuperClass):
def method(self):
print("method in replacer")
class Extender(SuperClass):
def method(self):
print('method in extender.')
SuperClass.method(self)
print('end in method')
class Provider(SuperClass):
def action(self):
print("action in provider.")
if __name__ == '__main__':
for clazz in (Inheritor, Replacer, Extender, Provider):
print("\t\t" + clazz.__name__ + " ...")
clazz().method()
x = Provider()
x.delegate()
输出如下:
method in replacer
Extender ...
method in extender.
method in suer class.
end in method
Provider ...
method in suer class.
action in provider.
Process finished with exit code 0
这种方式实现了一个简单的抽象超类的模型,但是还不是真正的抽象超类。因为抽象,意味着该类不能被实例化。而上述的超类是可以被实例化的。
改进如下:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @author : cat
# @date : 2017/6/21.
from abc import ABCMeta, abstractmethod
class SuperClass(metaclass=ABCMeta):
def method(self):
print("method in super class.")
def delegate(self):
self.action()
@abstractmethod
def action(self):
assert False, 'sub must implement this action'
class Inheritor(SuperClass):
pass
class Replacer(SuperClass):
def method(self):
print("method in replacer")
class Extender(SuperClass):
def method(self):
print('method in extender.')
SuperClass.method(self)
print('end in method')
class Provider(SuperClass):
def action(self):
print("action in provider.")
if __name__ == '__main__':
for clazz in (Inheritor, Replacer, Extender, Provider):
pass
# print("\t\t" + clazz.__name__ + " ...")
# clazz().method()
x = Provider()
x.delegate()
s = Inheritor()
输出如下:
Traceback (most recent call last):
action in provider.
File "/Users/cat/PycharmProjects/py01/super-pack/inherit.py", line 49, in <module>
s = Inheritor()
TypeError: Can't instantiate abstract class Inheritor with abstract methods action
通过给超类设置
metaclass = ABCMeta
以及,在需要让子类实现的方法上添加注解@abstractmethod
即可实现抽象超类。即:让该类不能被实例化。
该类的子类上可以被实例化的,前提是,重写了超类中被@abstractmethod
注解过的方法。
参考资料:《Learning Python》
更多推荐
已为社区贡献4条内容
所有评论(0)