实验7 类和模块

一、实验任务

user.py:

 1 class User:
 2     def __init__(self,name='guest',password='1'*6,status=1):
 3         self.name=name
 4         self.password=password
 5         self.status=status
 6     def info(self):
 7         if self.status==1:
 8             print(f'{self.name},{self.password},正常账户')
 9         if self.status==0:
10             print(f'{self.name},{self.password},封禁账户')
11     def modify_password(self):
12         n=0
13         while (n<3):
14             x=input('请输入旧密码:')
15             if x==self.password:
16                 y=input('请输入新密码:')
17                 self.password=y
18                 print('密码修改成功')
19                 break
20             else:
21                 n=n+1
22                 if n==3:
23                     print('账户已锁,请稍后再试')
24 class Admion(User):
25     def __init__(self,name='admin',password='9'*6,stuatus=1):
26         super().__init__(name, password,stuatus)
27     def info(self):
28         if self.status==1:
29             print(f'{self.name},{self.password},正常账户')
30         if self.status==0:
31             print(f'{self.name},{self.password},封禁账户')
32     def reset_password(self,User):
33         User.password='111111'
34     def ban_user(self,User):
35         User.status=0
36     def unblock_user(self,User):
37         User.status=1
38 
39 # 测试类
40 def main():
41     u1=User('Kate','111111',1)
42     u1.info()
43     u1.modify_password()
44     u1.info()
45     admion=Admion('Bob')
46     admion.info()
47     admion.reset_password(u1)
48     admion.ban_user(u1)
49     u1.info()
50     admion.unblock_user(u1)
51     u1.info()
52 if __name__ == '__main__':
53     main()

运行截图:

task5.py:

 1 from user import User
 2 u1=User('kate','111236',1)
 3 u1.info()
 4 u1.modify_password()
 5 
 6 
 7 from user import Admion
 8 a1=Admion('Bob','999999',1)
 9 a1.info()
10 a1.ban_user(u1)
11 u1.info()
12 a1.unblock_user(u1)
13 u1.info()
14 a1.reset_password(u1)
15 u1.info()

运行截图:

二、实验总结

通过本次实验我知道了什么是模块、包,也学会正确从包中导入模块,或者,从模块中导入函数、类。

 

posted @ 2022-06-02 23:20  Dimple$  阅读(11)  评论(1编辑  收藏  举报