class User(): """用户简介""" def __init__(self, first_name, last_name, gender, age): self.first_name = first_name self.last_name = last_name self.gender = gender self.age = age self.login_attempts = 0 def describe_user(self): """打印用户基本信息""" print("\n" + self.first_name.title() + ' ' + self.last_name.title() + " is " + self.gender.title() + ".") if self.gender == 'male': print("He is " + str(self.age) + " years old.") else: print("She is " + str(self.age) + " years old.") def greet_user(self): """向每位用户打招呼""" print("Hello, " + self.first_name.title() + ' ' + self.last_name.title() + "!") def increment_login_attempts(self): """将用户尝试登录次数加1""" self.login_attempts += 1 def reset_login_attempts(self): """将用户尝试登录次数重置为0""" self.login_attempts = 0 def Privileges(): """专门创建一个privileges的类""" def __init__(self): """初始化属性""" self.privileges = ['can add post', 'can delete post', 'can ban user'] def show_priviledge(self): """显示管理员有哪些特权""" print("The 'Admin' has the privileges as below:") for priviledge in self.privileges: print(priviledge) class Admin(User): """创建一个子类,继承父类""" def __init__(self, first_name, last_name, gender, age): """ 初始化父类属性 再初始化子类特有属性 """ super().__init__(first_name, last_name, gender, age) self.priviledge = Privileges() admin = Admin('jim', 'green', 'male', 29)admin.priviledge.show_priviledge()
1 回答
战渣渣
TA贡献12条经验 获得超3个赞
Privileges 这个你定义成函数 def Privileges
看你的意思是想把它定义成一个类吧。
定义成函数Privileges,执行Privileges()没有返回结果,所以返回的是None,所以你执行就报错了。
添加回答
举报
0/150
提交
取消