声明银行账户类,成员变量包括账号、储户姓名、开户时间、身份证号码、 存款余额等账户信息,成员方法包括开户、存款、取款、查询(余额、明细)、 销户等操作。
时间: 2023-11-08 18:49:31 浏览: 254
Java设计一个银行帐户类(继承与多态)
5星 · 资源好评率100%
```
class BankAccount:
def __init__(self, account_number, customer_name, id_number):
self.account_number = account_number
self.customer_name = customer_name
self.id_number = id_number
self.create_time = datetime.datetime.now()
self.balance = 0
self.transactions = []
def deposit(self, amount):
self.balance += amount
transaction = {"type": "deposit", "amount": amount, "time": datetime.datetime.now()}
self.transactions.append(transaction)
def withdraw(self, amount):
if amount > self.balance:
print("Insufficient funds.")
else:
self.balance -= amount
transaction = {"type": "withdraw", "amount": amount, "time": datetime.datetime.now()}
self.transactions.append(transaction)
def get_balance(self):
return self.balance
def get_transactions(self):
return self.transactions
def close_account(self):
self.account_number = None
self.customer_name = None
self.id_number = None
self.create_time = None
self.balance = None
self.transactions = None
def __str__(self):
return f"Account Number: {self.account_number}\nCustomer Name: {self.customer_name}\nID Number: {self.id_number}\nCreate Time: {self.create_time}\nBalance: {self.balance}"
```
使用方法:
```
# 开户
account1 = BankAccount("123456", "John Doe", "123456789012345678")
# 存款
account1.deposit(500)
# 取款
account1.withdraw(200)
# 查询余额
print(account1.get_balance())
# 查询明细
print(account1.get_transactions())
# 销户
account1.close_account()
```
阅读全文