栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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
 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/325741.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号