建立 mortgage 計算機類別
你一開始有一個能執行基本四則運算的 BasicCalculator。
接著你建立了 FinancialCalculator,它繼承自 BasicCalculator。你替這個類別新增了一個方法 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)