设计一个表示用户的User类,类中的变量有用户名、口令和记录用户个数的变量,定义类的构造方法、获取和设置用户名、口令的方法和返回帐户信息(第几个帐户,帐户名,密码)的方法
时间: 2024-05-03 08:19:57 浏览: 89
1.编写一个类实现银行帐户的概念,包括的属性有“帐号
5星 · 资源好评率100%
```python
class User:
count = 0
def __init__(self, username, password):
self.__username = username
self.__password = password
User.count += 1
def get_username(self):
return self.__username
def set_username(self, new_username):
self.__username = new_username
def get_password(self):
return self.__password
def set_password(self, new_password):
self.__password = new_password
def get_account_info(self):
return f"Account {User.count}: Username - {self.__username}, Password - {self.__password}"
```
使用示例:
```python
user1 = User("Alice", "123456")
print(user1.get_account_info()) # Account 1: Username - Alice, Password - 123456
user2 = User("Bob", "abcdef")
print(user2.get_account_info()) # Account 2: Username - Bob, Password - abcdef
user1.set_username("Alicia")
user1.set_password("654321")
print(user1.get_account_info()) # Account 1: Username - Alicia, Password - 654321
```
阅读全文