由于python没有抽象类、接口的概念,所以要实现这种功能得abc.py
这个类库,具体方式如下:
# coding: utf-8
import abc#抽象类
class StudentBase(object): __metaclass__ = abc.ABCMeta@abc.abstractmethod
def study(self): passdef play(self):
print("play")# 实现类
class GoodStudent(StudentBase): def study(self): print("study hard!") if __name__ == '__main__': student = GoodStudent() student.study() student.play()