Python编程从入门到实践 课后题 9-7 管理员

9-7 管理员 :管理员是一种特殊的用户。编写一个名为 Admin 的类,让它继承你为 
完成练习 9-3 或练习 9-5 而编写的 User 类。添加一个名为 privileges 的属性,用于存 
储一个由字符串(如”can add post”、 “can delete post”、 “can ban user”等)组成的 
列表。编写一个名为 show_privileges()的方法,它显示管理员的权限。创建一个 Admin 
实例,并调用这个方法。 

#9-3
class User():
    def __init__(self, first_name, last_name, address):
        self.first_name = first_name
        self.last_name = last_name
        self.address = address

    def describe_user(self):
        print('First name: ' + self.first_name.title())
        print('Last name: ' + self.last_name.title())
        print('Address: ' + self.address.title())

    def greet_user(self):
        self.full_name = self.first_name.title() + ' ' + self.last_name.title()
        print("Welcome comt to enjoy us, " + self.full_name.title() + "!")

alice = User('alice', 'anmay', 'xian')
alice.describe_user()
alice.greet_user()

#9-7
class Admin(User):
    def __init__(self , first_name , last_name , address): 
#注意这里在程序里面是两个“——”才可以,否则无法识别成字符串
        super().__init__(first_name , last_name , address)
        self.privileges = ['can add post', 'can delete post', 'can ban user']

    def show_privileges(self):
        for n in self.privileges:
             print("This admin " + n)

print("\n-----------")
my_alice = Admin('alice', 'anmay', 'xian')
my_alice.describe_user()
my_alice.greet_user()
my_alice.show_privileges()

结果

First name: Alice
Last name: Anmay
Address: Xian
Welcome comt to enjoy us, Alice Anmay!

-----------
First name: Alice
Last name: Anmay
Address: Xian
Welcome comt to enjoy us, Alice Anmay!
This admin can add post
This admin can delete post
This admin can ban user

Process finished with exit code 0
 

你可能感兴趣的:(python编程从入门到实践,python)