9-7 管理员
作者:互联网
1. 项目
管理员是一种特殊的用户。编写一个名为 Admin 的类,让它继承你为
完成练习 9-3 或练习 9-5 而编写的 User 类。添加一个名为 privileges 的属性,用于存
储一个由字符串(如"can add post"、 "can delete post"、 "can ban user"等)组成的
列表。编写一个名为 show_privileges()的方法,它显示管理员的权限。创建一个 Admin
实例,并调用这个方法。
2. 代码
class User(): # 创建User类 def __init__(self, first_name, last_name, age, address, phone): # 属性 self.first_name = first_name self.last_name = last_name self.age = age self.address = address self.phone = phone self.login_attempts = 0 # 添加属性 def describe_user(self): # 方法 print(self.first_name, self.last_name, self.age, self.address, self.phone) def greet_user(self): # 方法 print("How beautiful name " + self.last_name + self.last_name, "\n too young, too simple", "your homeland " + self.address + " is a warm place, ", "could you tell me your contact?") def increment_login_attempts(self, number1): # 递增方法 self.login_attempts += number1 # 值加1 print("共登录用户量:" + str(self.login_attempts)) def reset_login_attempts(self, number2): # 重置方法,重置为0 self.login_attempts = 0 print("共登录用户量:" + str(self.login_attempts)) # 创建Admin类 class Admin(User): # 初始化父类的属性,在初始化User特有的属性 def __init__(self, first_name, last_name, age, address, phone): super().__init__(first_name, last_name, age, address, phone) self.privileges = [] #初始化特有属性 def show_privileges(self): # 创建方法存储字符串 for privilege in self.privileges: print(privilege) admin = Admin('Kevin', 'Hou', 30, 'ShangHai', 13141516170) admin.describe_user() admin.privileges = ['can add post', 'can delete post', 'can ban user'] admin.show_privileges()
3. 执行结果
D:\python编程:从入门到实践\venv\Scripts\python.exe "D:/python编程:从入门到实践/9-7 管理员.py" Kevin Hou 30 ShangHai 13141516170 can add post can delete post can ban user Process finished with exit code 0
标签:last,name,self,attempts,管理员,address,login 来源: https://www.cnblogs.com/kevin-hou1991/p/14801305.html