定义一个名为account的类实现账户管理
时间: 2023-06-05 07:47:07 浏览: 373
java实现简单银行管理系统
account类可以包含以下属性和方法:
属性:
- 账户名
- 密码
- 余额
方法:
- 登录:输入账户名和密码,验证是否正确
- 存款:输入存款金额,将余额增加相应的数值
- 取款:输入取款金额,将余额减少相应的数值
- 查询余额:返回当前余额
示例代码:
class Account:
def __init__(self, username, password, balance):
self.username = username
self.password = password
self.balance = balance
def login(self, username, password):
if self.username == username and self.password == password:
return True
else:
return False
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
if self.balance >= amount:
self.balance -= amount
else:
print("余额不足")
def check_balance(self):
return self.balance
# 示例使用
my_account = Account("张三", "123456", 100)
if my_account.login("张三", "123456"):
my_account.deposit(500)
my_account.withdraw(200)
print("当前余额:", my_account.check_balance())
else:
print("登录失败")
阅读全文