实现抽象基类
现在您已经构建了 Company 抽象基类,它可以作为不同公司的"蓝图"来使用。您将通过创建一个 Technology 类来进行练习。
前面练习中的 Company 类已为您定义,如下所示:
class Company(ABC):
@abstractmethod
def create_budget(self):
pass
def hire_employee(self, name):
print(f"Welcome to the team, {name}!")
本练习是课程的一部分
Python 面向对象编程进阶
练习说明
- 创建一个继承自
Company抽象基类的Technology类。 - 定义带有两个参数
year和expenses的create_budget()方法。 - 使用名称 "Tina's Tech Advisors" 创建一个
Technology类的实例。 - 使用提供的参数调用
create_budget()方法;观察该方法的输出,以及hire_employee()的输出。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Create a class with the name "Technology"
class ____(____):
def __init__(self, name):
self.name = name
# Define a create_budget() method
def ____(self, ____, ____):
for expense, amount in expenses.items():
print(f"{year} budget for {expense} is {amount}")
# Create an instance of the Technology class, call methods
t = ____("____")
t.____(2024, {"Salaries": 10000, "Supplies": 500})
t.____("Christian")