9_8
1 # # coding=GBK 2 class User(): 3 """用户的信息""" 4 def __init__ (self , first_name , last_name): 5 """初始化属性first_name和last_name""" 6 self.first_name = first_name 7 self.last_name = last_name 8 self.login_attempts = 0 9 10 def describe_user(self): 11 """对用户进行描述""" 12 print("\n" + self.first_name.title() + " " + self.last_name.title() + " is our user .") 13 14 def greet_user(self): 15 """欢迎用户""" 16 print("Hello, " + self.first_name.title() + " " + self.last_name.title() + " .") 17 18 def f_attempts(self): 19 """原始的属性值""" 20 print("The login number is:" + str(self.login_attempts) + ".") 21 22 def increment_login_attempts(self ,attempts): 23 """增加1点属性值""" 24 self.login_attempts += attempts 25 26 def reset_login_attempts(self): 27 """重置属性值为0""" 28 self.login_attempts = 0 29 30 class Privileges(): 31 """创建一个Privileges的类""" 32 def __init__ (self , privileges): 33 """添加属性privileges""" 34 self.privileges = [] 35 36 def show_privileges(self): 37 """将方法show_privileges移入""" 38 print("Adimin can: ") 39 for privileges in self.privileges: 40 print("- " + privileges) 41 42 class Admin(User): 43 """管理员账号""" 44 def __init__ (self , first_name , last_name): 45 """ 46 初始化用户信息 47 展示管理员信息 48 """ 49 super(). __init__ (first_name , last_name) 50 self.privileges = [] 51 self.privileges = Privileges() 52 53 def show_privileges(self): 54 """显示管理员权限""" 55 print("Admin can: ") 56 for privileges in self.privileges: 57 print("- " + privileges) 58 59 you = Admin('Euruse' , 'Depp') 60 print(you.greet_user()) 61 62 you.privileges = ['can add post' , 'can delete post' , 'can ban user'] 63 you.show_privileges() 64 65 you.privileges.show_privileges()
修改后:
1 # # coding=GBK 2 class User(): 3 """用户的信息""" 4 def __init__ (self , first_name , last_name): 5 """初始化属性first_name和last_name""" 6 self.first_name = first_name 7 self.last_name = last_name 8 self.login_attempts = 0 9 10 def describe_user(self): 11 """对用户进行描述""" 12 print("\n" + self.first_name.title() + " " + self.last_name.title() + " is our user .") 13 14 def greet_user(self): 15 """欢迎用户""" 16 print("Hello, " + self.first_name.title() + " " + self.last_name.title() + " .") 17 18 def f_attempts(self): 19 """原始的属性值""" 20 print("The login number is:" + str(self.login_attempts) + ".") 21 22 def increment_login_attempts(self ,attempts): 23 """增加1点属性值""" 24 self.login_attempts += attempts 25 26 def reset_login_attempts(self): 27 """重置属性值为0""" 28 self.login_attempts = 0 29 30 class Admin(User): 31 """管理员账号""" 32 def __init__ (self , first_name , last_name): 33 """ 34 初始化用户信息 35 展示管理员信息 36 """ 37 super(). __init__ (first_name , last_name) 38 self.privileges = Privileges() 39 """将权限初始化化为空""" 40 41 class Privileges(): 42 """创建一个Privileges的类""" 43 def __init__ (self , privileges = []): 44 """添加属性privileges""" 45 self.privileges = privileges 46 47 def show_privileges(self): 48 """将方法show_privileges移入""" 49 print("Adimin can: ") 50 if self.privileges: 51 for privileges in self.privileges: 52 print("- " + privileges) 53 else: 54 print("-The user has no privileges.") 55 56 you = Admin('Euruse' , 'Depp') 57 you.greet_user() 58 59 you.privileges.show_privileges() 60 61 you_privileges = ['can add post' , 'can delete post' , 'can ban user'] 62 63 you.privileges.privileges = you_privileges 64 you.privileges.show_privileges()