其他分享
首页 > 其他分享> > 实验7

实验7

作者:互联网

class User:
    """用户"""
    count = 0

    def __init__(self, name='guest', password=111111, status=1):
        User.count += 1
        self.name = name
        self.password = password
        self.status = status

    def info(self):
        if self.status == 1:
            print(f'{self.name},{self.password} 账户正常')
        else:
            print(f'{self.name},{self.password} 账户被封禁')

    def modify_password(self):
        """修改密码"""
        i = 0
        while i < 3:
            n = input("请输入旧密码:")
            if eval(n) == self.password:
                n = eval(input("输入新密码:"))
                print('密码修改成功!')
                self.password = n
                break
            else:
                i += 1
        else:
            print("账户已锁住,稍后再试")
            self.status = 0

    @classmethod  # 修饰符
    def show_count(cls):
        print(f"用户总数:{User.count}")

    def __del__(self):
        User.count -= 1


class Admin(User):
    """管理者"""

    def __init__(self, name='admin', password=999999, status=1):
        super().__init__(name, password, status)

    def info(self):
        print("管理者账户")
        super().info()

    def ban_user(self, user):
        user.status = 0

    def unblock_user(self, user):
        user.status = 1

    def reset_password(self, user):
        user.modify_password()


if __name__ == "__main__":
    u1 = User()
    u1.info()

    admin1 = Admin()
    admin1.info()
    User.show_count()

    admin1.ban_user(u1)
    u1.info()
    admin1.unblock_user(u1)
    u1.info()
    admin1.reset_password(u1)
    del u1
    User.show_count()

以上为user.py源码及运行截图

from Admin_User import User,Admin

u1 = User()
u1.info()
u1.modify_password()
u1.info()

admin1 = Admin()
admin1.reset_password(u1)
u1.info()
admin1.ban_user(u1)
u1.info()
admin1.unblock_user(u1)
u1.info()

以上为task5.py源码及截图

实验总结:
super().__init__()是子类调用父类的属性相当于self.name = name,self.password=password...

super().方法 是子类调用父类的方法 在不添加形参时默认值应该时self,但不需要写

 

标签:__,info,self,u1,实验,user,password
来源: https://www.cnblogs.com/zw-com/p/16334679.html