9-7 管理员 : 管理员是一种特殊的用户。 编写一个名为Admin 的类, 让它继承你为完成练习9-3或练习9-5而编写的User 类。 添加一个名为privileges 的属性, 用
于存储一个由字符串(如"can add post" 、 “can delete post” 、 “can ban user” 等) 组成的列表。 编写一个名为show_privileges() 的方法, 它
显示管理员的权限。 创建一个Admin 实例, 并调用这个方法。
class User():
"""Represent a simple user profile."""
def __init__(self, first_name, last_name, username, email, location):
"""Initialize the user."""
self.first_name = first_name.title()
self.last_name = last_name.title()
self.username = username
self.email = email
self.location = location.title()
self.login_attempts = 0
def describe_user(self):
"""Display a summary of the user's information."""
print("\n" + self.first_name + " " + self.last_name)
print(" Username: " + self.username)
print(" Email: " + self.email)
print(" Location: " + self.location)
def greet_user(self):
"""Display a personalized greeting to the user."""
print("\nWelcome back, " + self.username + "!")
def increment_login_attempts(self):
"""Increment the value of login_attempts."""
self.login_attempts += 1
def reset_login_attempts(self):
"""Reset login_attempts to 0."""
self.login_attempts = 0
class Admin(User):
def __init__(self, first_name, last_name, username, email, location='广东'):
super().__init__(first_name, last_name, username, email, location)
self.privileges = []
def show_privileges(self):
print("\nPrivileges:")
for privilege in self.privileges:
print(privilege)
Admin = Admin('chen','xian','arc','123@qq.com')
Admin.privileges = ["can add post" ,
"can delete post" ,
"can ban user",
]
Admin.show_privileges()
Admin.describe_user()
版权声明:本文为Arclight869原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。