编写一个bankacoount类,包含属性account_number和balance,提供方法deposit(account)和without(account)来处理存款和取款操作,并在取款时检查余额是否足够
时间: 2024-12-09 16:17:55 浏览: 18
好的,这是一个简单的BankAccount类示例,它有`account_number`和`balance`两个属性,以及`deposit`和`withdraw`两个方法:
```python
class BankAccount:
def __init__(self, account_number, initial_balance=0):
self.account_number = account_number
self.balance = initial_balance
def deposit(self, amount):
"""
存款操作,增加账户余额
:param amount: 要存入的金额
"""
if amount > 0:
self.balance += amount
else:
print("请输入正整数作为存款金额")
def withdraw(self, amount):
"""
取款操作,检查余额充足后减少账户余额
:param amount: 要取出的金额
"""
if amount <= self.balance:
self.balance -= amount
else:
print("余额不足,无法完成取款")
# 示例
account = BankAccount(123456789)
account.deposit(1000)
account.withdraw(500)
阅读全文