实验7 类和模块

Posted on 2022-06-06 22:52  灯下白头人  阅读(20)  评论(2编辑  收藏  举报

实验内容

实验任务1-4 略

实验任务5

源代码

 1 """
 2 Username moudle
 3 including:
 4 base class: User
 5 brunch class: Admin
 6 """
 7 
 8 class User:
 9     """Username base class"""
10     def __init__(self,name="guest",password="111111",status=1):
11         self._name=name
12         self._password=password
13         self._status=status
14 
15     def info(self):
16         print(f"Username:{self._name}")
17         print(f"Password:{self._password}")
18         if self._status==1:
19             print("User Status: normal")
20         else:
21             print("User Status: freezed")
22 
23     def modify_password(self):
24         i=1
25         while i<=3:
26             password=input("Please enter your old password:")
27             if password==self._password:
28                 break
29             else:
30                 print("Password is wrong!");i+=1
31         if i>3:
32             self._status=0
33             print("Your account is freezed.Please try later.")
34         else:
35             password=input("Please enter your new account:")
36             self._password=password
37             print("Password changed successfully!")
38 
39 class Admin(User):
40     """Administrator class"""
41     def __init__(self,name="Admin",password="999999",status=1):
42         self._name=name
43         self._password=password
44         self._status=status
45 
46     def reset_password(self,user):
47         if type(user)!=User:
48             raise KeyError
49         a=input("Testing your Admin status(enter your name):")
50         if not(self._name==a and self._status==1):
51             print("You have no right to do so!")
52         else:
53             user._password="111111"
54             print("User's password is successfully resetted!")
55 
56     def ban_user(self,user):
57         if type(user)!=User:
58             raise KeyError
59         a=input("Testing your Admin status(enter your name):")
60         if not(self._name==a and self._status==1):
61             print("You have no right to do so!")
62         else:
63             if user._status==0:
64                 print("User has been freezed!")
65             else:
66                 user._status=0
67                 print("User's account has successfully freezed!")
68 
69     def unblock_user(self,user):
70         if type(user)!=User:
71             raise KeyError
72         a=input("Testing your Admin status(enter your name):")
73         if not(self._name==a and self._status==1):
74             print("You have no right to do so!")
75         else:
76             if user._status==1:
77                 print("User is normal!")
78             else:
79                 user._status=1
80                 print("User's account has successfully unfreezed!")
81 
82 def main():
83     u=User()
84     u.info()
85     print()
86     a=Admin()
87     a.info()
88 
89 if __name__=="__main__":
90     main()

运行截图

*中间报错为调试环节。已修改至无误。

*在基础要求上加入了对管理员类内方法传输对象的判定以及封禁解封账户的重复操作提醒。

  对类的判定并不轻松。因为运行时相当于直接导入了user模块,即等效于from user import *,故可以直接如题目中用type...= user 判定。若在命令行以模块形式单独导入,理论上会报错,因为type返回值为user.User。但实际运行并无错误。

 

官方文档中的类方法有待探索。

实验总结

类可以简单地视为封装好的,带有操作方法的数据。类应当可以导入数据作为对象。有待尝试。