建立一个银行账号类,并提供存取操作
时间: 2023-05-20 11:05:06 浏览: 78
好的,以下是一个简单的银行账号类的代码示例:
```python
class BankAccount:
def __init__(self, account_number, balance=0):
self.account_number = account_number
self.balance = balance
def deposit(self, amount):
self.balance += amount
print(f"成功存入 {amount} 元,当前余额为 {self.balance} 元。")
def withdraw(self, amount):
if amount > self.balance:
print("余额不足,无法取款。")
else:
self.balance -= amount
print(f"成功取出 {amount} 元,当前余额为 {self.balance} 元。")
```
你可以使用这个类来创建银行账号对象,并进行存取操作。例如:
```python
my_account = BankAccount("1234567890")
my_account.deposit(1000) # 存入 1000 元
my_account.withdraw(500) # 取出 500 元
```
输出:
```
成功存入 1000 元,当前余额为 1000 元。
成功取出 500 元,当前余额为 500 元。
```
希望这个示例对你有所帮助!