接口的作用
抽象类
from abc import ABC,abstractmethod
'''
抽象类需要导入abc模块继承ABC类,需要有抽象方法作为子类继承的规范,
@abstractmethod装饰器用来装饰抽象方法
'''
class Student(ABC):
@abstractmethod
def eat_breakfast(self):
pass
实现接口作用
from abstruct_class import Student
class DomesticStudent(Student): # 继承抽象类后必须实现抽象方法
def eat_breakfast(self):
print("早饭吃的是:豆浆,胡辣汤,烤全羊")
class InternationalStudent(Student):
def eat_breakfast(self):
print("早饭吃的是:汉堡,可乐等垃圾食品")
def eat_breakfast(student: Student):
student.eat_breakfast()
def main():
ds1 = DomesticStudent()
is1 = InternationalStudent()
eat_breakfast(ds1)
eat_breakfast(is1)
if __name__ == "__main__":
main()