为继承的类添加方法
让我们为 MortgageCalculator 添加一个方法,用于在给定本金、年利率和还款年数的情况下计算每月还款额。
本练习是课程的一部分
案例研究:用 Python 构建软件
交互式实操练习
通过完成这段示例代码来试试这个练习。
class MortgageCalculator(FinancialCalculator):
def __init__(self, loan_amount, annual_interest_rate, years):
super().__init__()
self.loan_amount = loan_amount
self.monthly_interest_rate = self.monthly_interest(annual_interest_rate)
self.months = self.multiply(years, 12)
self.monthly_payment = self.calculate_monthly_payment()
def calculate_monthly_payment(self):
numerator = self.monthly_interest_rate * (1 + self.monthly_interest_rate) ** self.months
denominator = (1 + self.monthly_interest_rate) ** self.months - 1
# Compute the quotient of numerator and denominator and save the result to multiplier
multiplier = self.divide(____, ____)