编写一个名为Privileges 的类,它只有一个属性——privileges ,其中存储了练习9-7 所说的字符串列表。将方法show_privileges() 移到这 个类中。在Admin 类中,将一个Privileges 实例用作其属性。创建一个Admin 实例,并使用方法show_privileges() 来显示其权限。
#!/iser/bin/env python
# coding:utf-8
class User(object):
def __init__(self,first_name,last_name,age,phone_number):
self.first_name=first_name
self.last_name=last_name
self.age=age
self.phone_number=phone_number
def describe_user(self):
full_name=self.first_name+" "+self.last_name
print(full_name+" is "+str(user_one.age)+" years old,and he's phone number is "+user_one.phone_number+".")
def greet_user(self):
full_name=self.first_name+" "+self.last_name
print("Hello,"+full_name+" !")
class Admin(User):
def __init__(self,first_name,last_name,age,phone_number):
super(Admin,self).__init__(first_name,last_name,age,phone_number)
self.privileges=Priviliges()
class Priviliges():
def __init__(self):
self.privileges=["can add post","can delete post","can ban user"]
def show_privileges(self):
print("The power of admin are:")
for privilege in self.privileges:
print(privilege)
user_one=User('Jackson','Yee',18,'1234567890')
user_one.describe_user()
user_one.greet_user()
admin=Admin('Jackson','Yee',18,'1234567890')
admin.privileges.show_privileges()
结果为: