编写程序,创建银行账户类Account,具有deposite(存款)和withdraw(取款),并编写测试代码。
时间: 2024-01-15 07:03:54 浏览: 19
好的,以下是Python的代码实现:
```python
class Account:
def __init__(self, balance=0):
self.balance = balance
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
if self.balance >= amount:
self.balance -= amount
else:
print("Insufficient funds")
# 测试代码
account = Account()
account.deposit(1000)
print(f"Balance after deposit: {account.balance}")
account.withdraw(500)
print(f"Balance after withdrawal: {account.balance}")
account.withdraw(600)
print(f"Balance after withdrawal: {account.balance}")
```
输出结果:
```
Balance after deposit: 1000
Balance after withdrawal: 500
Insufficient funds
Balance after withdrawal: 500
```
在上面的代码中,我们定义了一个名为Account的类,该类具有两个方法:deposit和withdraw,用于存款和取款。在类的构造函数中,我们将余额初始化为0。在存款方法中,我们将传递的金额添加到当前余额中。在取款方法中,我们检查当前余额是否大于等于要取款的金额,如果是,则从余额中减去该金额,否则打印“余额不足”的消息。最后,我们编写了一些测试代码,测试存款和取款方法的功能。