07 ### 面向对象封装场景

# 用户注册案例:未封装场景
"""
user_list = [
    {"name":"test","password":"1234},
    {"name":"taaa","password":"1234},
]
"""

user_list = []
while True:
    user = input("请输入用户名:").strip()
    pwd = input("请输入密码:").strip()

    if pwd.isupper() == 'Q':
        break
    info = {"name": user, "password": pwd}
    user_list.append(info)

for item in user_list:
    message = f"我的名字{item['name']},我的密码{item['password']}"


# 面向对象
class userInfo:

    def __init__(self, name, password):
        self.name = name
        self.password = password

    def show(self):
        message = f"我的名字{item.name},我的密码{item.password}"
        print(message)


user_list = []
while True:
    user = input("请输入用户名:").strip()
    pwd = input("请输入密码:").strip()

    if pwd.isupper() == 'Q':
        break
    info = userInfo(user, pwd)
    user_list.append(info)

for item in user_list:
    item.show()

posted @ 2024-09-27 06:10  jhchena  阅读(8)  评论(0编辑  收藏  举报