开始使用免费开始使用

创建按揭计算器类

您从能执行基本算术运算的 BasicCalculator 开始。

随后,您创建了继承自 BasicCalculatorFinancialCalculator。您通过添加一个方法 monthly_interest() 来扩展该类,用于根据年利率计算月利率。

现在是创建 MortgageCalculator 类的时候了!

本练习是课程的一部分

案例研究:用 Python 构建软件

查看课程

练习说明

  • 在不直接调用父类名称的情况下实例化父类的方法和属性。
  • 使用相应的参数初始化 self.loan_amount
  • 使用继承的方法初始化 self.monthly_interest_rate

交互式实操练习

通过完成这段示例代码来试试这个练习。

class MortgageCalculator(FinancialCalculator):
  def __init__(self, loan_amount, annual_interest_rate, years):
    # Initialize the parent attributes and functions
    ____
    
	# Initialize the loan_amount attribute
    self.loan_amount = ____
    
    # Initialize the monthly_interest_rate
    self.monthly_interest_rate = self.____(annual_interest_rate)
编辑并运行代码