python 抽象类、抽象方法的使用

接口的作用

抽象类

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值