from abc import ABCMeta, abstractmethod
class People(metaclass=ABCMeta):
def __init__(self, name):
self.name = name
@abstractmethod
def show_name(self):
pass
class Student(People):
def show_name(self):
print('Student: {0}'.format(self.name))
class Teacher(People):
def show_name(self):
print('Teacher: {0}'.format(self.name))
if __name__ == '__main__':
# TypeError: Can't instantiate abstract class People with abstract methods show_name
# p = People("Python")
student = Student("Li")
student.show_name()
teacher = Teacher("Wang")
teacher.show_name()
本系列文章和代码已经作为项目归档到github,仓库地址:jumper2014/PyCodeComplete。大家觉得有帮助就请在github上star一下,你的支持是我更新的动力。什么?你没有github账号?学习Python怎么可以没有github账号呢,快去注册一个啦!