FinancialCalculator
Hãy mở rộng lớp BasicCalculator và tạo FinancialCalculator.
Đầu tiên, bạn sẽ tạo lớp con này với chức năng tối thiểu: khả năng tính lãi suất theo tháng từ lãi suất theo năm. Sau đó, bạn sẽ bổ sung thêm nhiều tính năng cho FinancialCalculator!
numpy đã được nhập với bí danh np cho bạn.
Bài tập này là một phần của khóa học
Nghiên cứu tình huống: Xây dựng phần mềm bằng Python
Hướng dẫn bài tập
- Tạo lớp
FinancialCalculatorbằng cách kế thừa từBasicCalculator. - Tạo một thể hiện của lớp
FinancialCalculatorlàfinancial_calculator. - Dùng
financial_calculatorđể tính lãi suất theo tháng với lãi suất theo năm là 6%, nhớ nhập phần trăm dưới dạng số thập phân.
Bài tập tương tác thực hành trực tiếp
Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.
class BasicCalculator():
def divide(self, x, y):
return x / y
# Create a class FinancialCalculator that inherits from BasicCalculator
class FinancialCalculator(____):
def calculate_monthly_interest_rate(self, annual_interest_rate):
return self.divide(annual_interest_rate, 12)
# Create a financial calculator
financial_calculator = ____()
# Print the monthly interest rate for a yearly rate of 6 percent
print(financial_calculator.calculate_monthly_interest_rate(____))