使用 ABC 构建正式接口
正式接口为接口与其实现类之间提供了更具约束力的"契约"。您将练习使用 abc 模块创建由两个抽象方法组成的正式接口。ABC 类和 abstractmethod 装饰器已从 abc 导入。玩得开心!
本练习是课程的一部分
Python 面向对象编程进阶
练习说明
- 创建一个名为
Product的正式接口。 - 将
purchase()定义为抽象方法,参数为self和quantity,并在方法体中加入关键字pass。 - 定义抽象方法
update_price(),参数为self和new_price。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Create a Product interface
____ ____(____):
# Define a purchase() abstract method
____
def ____(____, quantity):
____
# Create an update_price() abstract method
____
def ____(self, ____):
____