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()
战渣渣
相关分类