实验7 类和模板

定义类: 
1
class User: 2 def __init__(self,name='guest',password='111111',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 else: 10 print(f'{self._name},{self._password},账号封禁') 11 def modify_password(self): 12 j=0 13 while True: 14 n=input('输入旧密码:') 15 if n==self._password: 16 self._password=eval(input('输入新密码:')) 17 print('密码修改成功') 18 self._status=1 19 break 20 else: 21 j+=1 22 if j==3: 23 print('账户已锁,请稍后再试') 24 break 25 class Admin(User): 26 def __init__(self,name='admin',password='999999',status='1'): 27 self._name=name 28 self._password=password 29 self._status=status 30 def reset_password(self,m): 31 m.password=input('输入修改的密码:') 32 def ban_user(self,m): 33 m.status='0' 34 def unblock_user(self,m): 35 m.status='1'
 task5.py:
1
def main(): 2 u1 = User('max', '1234', '1') 3 u1.info() 4 u1.modify_password() 5 u1.info() 6 7 admin1 = Admin('min', '3456', '1') 8 admin1.info() 9 admin1.reset_password(u1) 10 admin1.ban_user(u1) 11 u1.info() 12 admin1.unblock_user(u1) 13 u1.info() 14 if __name__=='__main__': 15 main()

 实验总结:

1、对定义类还不是太熟悉,一些用法不够熟练;

2、需要再多多学习。

posted @ 2022-06-01 23:34  暮渎  阅读(23)  评论(0编辑  收藏  举报