实验七:类和模块

class User():
    name='guest'
    password='111111'
    def __init__(self,name,password):
        self.name=name
        self.password=password
        self.status=1
    def info(self):
        if self.status==1:
            print(self.name,self.password,'状态正常')
        else:
            print(self.name,self.password,'封禁')
    def modify_password(self):
        n=0
        while n<3:
            old=input('输入你的密码')
            if self.password!=old:
                print('密码错误')
                n=n+1
            else:
                new=input('输入你的新密码')
                self.password=new
                print(self.name,'密码修改成功')
                break
        else:
            self.status=0
            print('账户已锁,请稍后再试')
x1=User('zhao','123465')
x1.info()
x1.modify_password()

 

 

class User():
    count = 0
    def __init__(self, username='username', password='111', status='1'):
        self.username = username
        self.password = password
        self.status = status
        User.count += 1

    def __del__(self):
        User.count -= 1

    def info(self):
        if self.status == '1':
            print(f"{self.username}, {self.password}, 账户正常")
        else:
            print(f"{self.username}, {self.password}, 账户被封禁")
    def modify_password(self):
        for i in range(3):
            old_password = input("旧密码: ")
            if old_password == self.password:
                self.password = input("新密码: ")
                print("密码修改成功!")
                break
            elif i == 2:
                print("账户已锁, 请稍后再试...")
                self.status = '0'
            elif old_password != self.password:
                continue

    def show_count():
        print(f"用户总数: {User.count}")


class Admin(User):
    def __init__(self, username='Admin', password='999999', status='1'):
        super().__init__(username, password, status)

    def info(self):
        print("管理员账户: ")
        super().info()
    def reset_password(self, user):
        user.password = input("请输入你要修改的密码: ")

    def ban_user(self, user):
        user.status = '0'

    def unlock_user(self, user_name):
        user_name.status = '1'


u1=User('账户1','0000','1')
u1.info()
u1.modify_password()
u1.info()

admin1=Admin('管理员1','9999','1')
admin1.info()
User.show_count()

admin1.reset_password(u1)
admin1.ban_user(u1)
u1.info()
admin1.unlock_user(u1)
u1.info()
User.show_count()

 

posted @ 2022-06-05 20:41  赵益浩  阅读(26)  评论(1编辑  收藏  举报