py设计一个Account类表示账户,自行设计该类中的属性和方法,并利用这个类创建一个账号为998866,余额为2000,年利率为4.5%的账户,然后从该账户中存入150,取出1500。打印出账号,余额,年利率,月利率,月息
时间: 2024-06-09 12:04:36 浏览: 55
以下是实现要求的代码:
```python
class Account:
def __init__(self, account_num, balance, annual_interest_rate):
self.__account_num = account_num
self.__balance = balance
self.__annual_interest_rate = annual_interest_rate
def get_account_num(self):
return self.__account_num
def get_balance(self):
return self.__balance
def get_annual_interest_rate(self):
return self.__annual_interest_rate
def set_annual_interest_rate(self, annual_interest_rate):
self.__annual_interest_rate = annual_interest_rate
def get_monthly_interest_rate(self):
return self.__annual_interest_rate / 12
def get_monthly_interest(self):
return self.__balance * self.get_monthly_interest_rate()
def withdraw(self, amount):
self.__balance -= amount
def deposit(self, amount):
self.__balance += amount
# 创建账户
account = Account('998866', 2000, 0.045)
# 存入150
account.deposit(150)
# 取出1500
account.withdraw(1500)
# 打印账户信息
print('账号:', account.get_account_num())
print('余额:', account.get_balance())
print('年利率:', account.get_annual_interest_rate())
print('月利率:', account.get_monthly_interest_rate())
print('月息:', account.get_monthly_interest())
```
输出结果如下:
```python
账号: 998866
余额: 650
年利率: 0.045
月利率: 0.00375
月息: 2.4375
```
阅读全文