python中硬要写抽象类和抽象方法

由于python没有抽象类、接口的概念,所以要实现这种功能得abc.py这个类库,具体方式如下:

# coding: utf-8
import abc

#抽象类
class StudentBase(object):
  __metaclass__ = abc.ABCMeta

  @abc.abstractmethod
  def study(self):
    pass

  def play(self):
    print("play")

# 实现类
class GoodStudent(StudentBase):
  def study(self):
    print("study hard!")


if __name__ == '__main__':
  student = GoodStudent()
  student.study()
  student.play()

posted @ 2019-04-29 21:48  Samuel-Leung  阅读(251)  评论(0编辑  收藏  举报