实验七 类和模块

user.py

 1 class User:
 2     def __init__(self,name,password,status):
 3         if name=="" and password=="":
 4             self.name="guest"
 5             self.password="111111"
 6             self.status="1"
 7         else:
 8             self.name=name
 9             self.password=password
10             self.status=status
11     def info(self):
12         print(f"用户名:{self.name}")
13         print(f"密码:{self.password}")
14         print(f"账号状态:{self.status}")
15     def modify_password(self):
16         flag=0
17         while(flag<3):
18             n=input("请输入原密码:")
19             if n==self.password:
20                 flag=4
21                 n1=input("请输入新密码:")
22                 self.password=n1
23                 print("密码修改成功")
24             else:
25                 flag+=1
26         if flag==3:
27             print("账户已锁,请稍后再试.")
28 class Admin(User):
29     def __init__(self,name,password,status):
30         if name=="" and password=="":
31             self.name="admin"
32             self.password="999999"
33             self.status="1"
34         else:
35             self.name=name
36             self.password=password
37             self.status=status
38     def reset_password(self,f):
39         f.password="111111"
40     def ban_user(self,f):
41         f.status="0"
42     def unblock_user(self,f):
43         f.status="1"
44 def main():
45     x = User("Bob", "12345", "1")
46     y = Admin("Alex", "1112345", "1")
47     x.info()
48     y.info()
49     y.reset_password(x)
50     x.info()
51     y.ban_user(x)
52     x.info()
53     y.unblock_user(x)
54     x.info()
55 if __name__ == '__main__':
56     main()

运行测试截图

 

 

task5.py

 1 from user import User
 2 from user import Admin
 3 x=User("","","")
 4 y=Admin("Alex","12345","1")
 5 x.info()
 6 y.info()
 7 x.modify_password()
 8 x.info()
 9 y.reset_password(x)
10 y.ban_user(x)
11 x.info()
12 y.unblock_user(x)
13 x.info()

运行测试截图

 

posted @ 2022-06-01 17:02  昏汐  阅读(19)  评论(2编辑  收藏  举报