7-4 设计一个bankaccount类
时间: 2023-04-27 19:02:41 浏览: 852
BankAccount
BankAccount类是一个用于表示银行账户的类。它应该包含以下属性和方法:
属性:
- account_number:账户号码
- balance:账户余额
- owner:账户所有者的姓名
方法:
- deposit(amount):向账户中存入指定金额
- withdraw(amount):从账户中取出指定金额
- get_balance():获取账户余额
- get_account_number():获取账户号码
- get_owner():获取账户所有者的姓名
以下是BankAccount类的示例代码:
```
class BankAccount:
def __init__(self, account_number, balance, owner):
self.account_number = account_number
self.balance = balance
self.owner = owner
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
if amount > self.balance:
print("Insufficient funds")
else:
self.balance -= amount
def get_balance(self):
return self.balance
def get_account_number(self):
return self.account_number
def get_owner(self):
return self.owner
```
使用示例:
```
# 创建一个账户
account = BankAccount("123456789", 1000, "张三")
# 存入100元
account.deposit(100)
# 取出200元
account.withdraw(200)
# 获取账户余额、账户号码和账户所有者的姓名
print(account.get_balance())
print(account.get_account_number())
print(account.get_owner())
```
阅读全文