实验7 类和模块

class User:
    def __init__(self,name='guest',password=111111,status=1):
        self.name=name
        self.password=password
        self.status=status
    def info(self):
        print('账户信息:用户名,密码,账号状态')
        if self.status==1:
            print(f'{self.name},{self.password},账号状态正常')
        elif self.status==0:
            print(f'{self.name},{self.password},账号状态封禁')
    def modify_password(self,count=1):
        while count<=3:
            old_password=eval(input('请先输入旧密码:'))
            if old_password==self.password:
                new_password=eval(input('请输入新密码:'))
                self.password=new_password
                print('密码修改成功')
                break
            else:
                count+=1
                if count==4:
                    print('账户已锁,请稍后再试')
                continue
class Admin(User):
    def __init__(self,name='admin',password=999999,stasus=1):
        super().__init__(name,password,stasus)
    def info(self):
        if self.status == 1:
            print(f'{self.name},{self.password},正常账号')
        elif self.status == 0:
            print(f'{self.name},{self.password},封禁账号')
    def reset_password(self,User):
        self.password=111111
    def ban_user(self,User):
        self.status=0
    def unblock_user(self,User):
        self.status=1
#测试
def main():
    a1=User('John',123456)
    a1.info()
    a1.modify_password()
    a1.info()
    a2=Admin('Bob',123456)
    a2.info()
    a2.reset_password(a1)
    a2.ban_user(a1)
    a2.unblock_user(a1)
    a2.info()
if __name__=='__main__':
    main()

from user import User
a1=User('Mimi',123456)
a1.info()
a1.modify_password()
from user import Admin
a2=Admin('Seho',147896)
a2.info()
a2.reset_password(User)
a2.info()
a2.ban_user(User)
a2.info()
a2.unblock_user(User)
a2.info()

 

posted @ 2022-06-08 13:16  孙艺欣  阅读(14)  评论(3编辑  收藏  举报