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()

原文地址:https://www.cnblogs.com/Samuel-Leung/p/10793112.html