สร้างคลาส MortgageCalculator
เราเริ่มต้นด้วย BasicCalculator ที่ทำการคำนวณเลขพื้นฐาน
จากนั้นสร้าง FinancialCalculator ที่รับช่วงมาจาก BasicCalculator และขยายความสามารถโดยเพิ่มเมธอด monthly_interest() สำหรับคำนวณดอกเบี้ยรายเดือนจากอัตราดอกเบี้ยรายปี
ถึงเวลาสร้างคลาส MortgageCalculator กันแล้ว!
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
กรณีศึกษา: การสร้างซอฟต์แวร์ด้วย Python
คำแนะนำการฝึกหัด
- เรียกใช้เมธอดและแอตทริบิวต์ของ parent โดยไม่ต้องระบุชื่อ parent โดยตรง
- Initialize
self.loan_amountโดยใช้พารามิเตอร์ที่เหมาะสม - ใช้เมธอดที่รับช่วงมาเพื่อ initialize
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)