管理员是一种特殊的用户。编写一个名为Admin 的类,让它继承你为完成练习9-3或练习9-5而编写的User 类。添加一个名为privileges 的属性,用 于存储一个由字符串(如"can add post" 、"can delete post" 、"can ban user" 等)组成的列表。编写一个名为show_privileges() 的方法,它 显示管理员的权限。创建一个Admin 实例,并调用这个方法。
class User:
def __init__(self, first_name, last_name, age):
self.first_name = first_name
self.last_name = last_name
self.age = age
self.full_name = ''
self.login_attempts = 0
def describe_user(self):
self.full_name = self.first_name.title() + " " + self.last_name.title()
print(self.full_name)
print(self.full_name + " is "+str(self.age)+" years old.")
def greet_users(self):
print(self.full_name+" nice to meet you!")
def increment_login_attempts(self):
for index in range(1, 10):
self.login_attempts += 1
print(self.login_attempts)
def reset_login_attempts(self):
self.login_attempts = 0
class Admin(User):
def __init__(self, first_name, last_name, age):
super().__init__(first_name, last_name, age)
self.privileges = []
def show_privileges(self):
print("The privileges are shown below:")
for privilege in self.privileges:
print(privilege)
user1 = Admin('zhang', 'san', 18)
user1.privileges = ['can add post', 'can delete post', 'can ban user']
user1.show_privileges()

本文介绍了一个Python程序,该程序定义了User类和继承自User的Admin类。Admin类包含一个存储管理员权限的列表,并提供一个方法来显示这些权限。通过创建Admin实例并调用show_privileges方法来演示权限的显示。
1091

被折叠的 条评论
为什么被折叠?



