开始使用免费开始使用

创建抽象基类

抽象基类是为一组相似的类创建"蓝图"的好方法。本练习中,您将创建一个名为 Company 的抽象基类,其中包含 1 个抽象方法和 1 个具体方法。祝您顺利!

本练习是课程的一部分

Python 面向对象编程进阶

查看课程

练习说明

  • abc 模块导入 ABCabstractmethod
  • 创建一个名为 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}!")
编辑并运行代码