创建抽象基类
抽象基类是为一组相似的类创建"蓝图"的好方法。本练习中,您将创建一个名为 Company 的抽象基类,其中包含 1 个抽象方法和 1 个具体方法。祝您顺利!
本练习是课程的一部分
Python 面向对象编程进阶
练习说明
- 从
abc模块导入ABC和abstractmethod。 - 创建一个名为
Company的抽象基类,并继承自ABC。 - 定义一个名为
create_budget()的抽象方法,但使用pass,不编写任何逻辑。 - 创建一个名为
hire_employee()的具体方法。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Import the ABC class and abstractmethod decorator from abc
from abc import ____, ____
# Define an abstract base class called Company
class ____(____):
# Create an abstract method called create_budget()
____
def ____(self):
____
# Create a concrete method with name hire_employee()
def ____(self, name):
print(f"Welcome to the team, {name}!")