python----抽象类

时间:2024-10-03 16:07:02
#!/usr/local/python3.5/bin/python3.5

####实现方法一
class Supper(object):
def delegate(self):
self.action() def action(self):
raise NotImplementedError("you must implement Supper.action function") class Sub(Supper):
pass ####实现方法二 from abc import ABCMeta
from abc import abstractmethod class Supper2(metaclass=ABCMeta):
def delegate(self):
self.action()
@abstractmethod
def action(self):
pass class Sub2(Supper2):
def action(self):
print('hello world!') if __name__=="__main__": s=Sub2()
s.delegate();