python从入门到实践 课后题 9-12 多个模块

python从入门到实践 课后题 9-12 多个模块,第1张

python从入门到实践 课后题 9-12 多个模块

储存User类的模块user

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 come to enjoy us, " + self.full_name.title() + "!")

储存Admin和Privileges的模块privileges_admin

from user import User #引入模块user中的User类
class Admin(User):
    def __init__(self , first_name , last_name , address):
        super().__init__(first_name , last_name , address)
        self.privileges = Privileges()

#9-8
class Privileges():
    def __init__(self):
        self.priviliges = ['can add post', 'can delete post', 'can ban user']
    def show_privileges(self):
        for n in self.priviliges:
             print("This admin " + n)

9-12的代码

from privileges_admin import Admin, Privileges
z = Admin('li', 'ming', 'xian')
z.privileges.show_privileges()

结果

This admin can add post
This admin can delete post
This admin can ban user

Process finished with exit code 0
 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/3973743.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-10-21
下一篇 2022-10-21

发表评论

登录后才能评论

评论列表(0条)

保存