请编写一个基于对象的程序,设计一个账户类。属性:账户名、密码(属性封装,密码不少于6位);行为:打印账户名及密码(密码加密输出,例如:ASCII+15)。并在main函数中,定义一个该类对象,友好输出
时间: 2023-05-31 19:04:31 浏览: 90
class Account:
def __init__(self, username, password):
self.__username = username
self.__password = password
def set_password(self, password):
if len(password) < 6:
print("Password should be at least 6 characters long")
else:
self.__password = password
def get_username(self):
return self.__username
def get_password(self):
return self.__password
def print_account_info(self):
print("Username:", self.__username)
print("Password:", ''.join([chr(ord(c) + 15) for c in self.__password]))
# Test the class
my_account = Account("my_username", "my_password123")
my_account.print_account_info() # Output: Username: my_username, Password:
my_account.set_password("short") # Output: Password should be at least 6 characters long
my_account.set_password("new_password")
my_account.print_account_info() # Output: Username: my_username, Password:
阅读全文