始める無料で始める

モーゲージ計算機クラスを作成する

まずは基本的な四則演算を行う BasicCalculator から始めました。

次に、この BasicCalculator を継承した FinancialCalculator を作成しました。このクラスでは、年利から月利を計算するメソッド 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)
コードを編集して実行