9-11 导入Admin 类 : 以为完成练习9-8而做的工作为基础, 将User 、 Privileges 和Admin 类存储在一个模块中, 再创建一个文件, 在其中创建一个Admin 实例
并对其调用方法show_privileges() , 以确认一切都能正确地运行。
模块 admin.py
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() + "!")
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-11部分
import admin
z = admin.Admin('明', '李', '西安')
z.privileges.show_privileges()
结果
This admin can add post
This admin can delete post
This admin can ban userProcess finished with exit code 0