说明
下面为课本上第九章动手试一试中的部分习题
9-3
题目:用户
创建一个名为User 的类,其中包含属性first_name 和last_name ,还有用户简介通常会存储的其他几个属性。在类User 中定义一个名为describe_user() 的方法,它打印用户信息摘要;再定义一个名为greet_user() 的方法,它向用户发出个性化的问候。创建多个表示不同用户的实例,并对每个实例都调用上述两个方法
code:
class User():
def __init__(self, first_name, second_name):
self.first_name = first_name
self.second_name = second_name
def describe(self):
print("This is " + self.first_name + " " + self.second_name)
def greet(self):
print("Hello, I'm " + self.first_name + " " + self.second_name)
zero = User('zero', 'Qiu')
daniel = User('Daniel', 'Wu')
jay = User('Jay', 'Chou')
zero.describe()
zero.greet()
daniel.describe()
daniel.greet()
jay.describe()
jay.greet()
result:
9-5
题目:尝试登录次数
在为完成练习9-3而编写的User类中,添加一个名为login_attempts的属性。编写一个名为
increment_login_attempts()的方法,它将属性login_attempts 的值加1。再编写一个名为reset_login_attempts() 的方法,它将属性login_attempts 的值重置为0。根据User 类创建一个实例,再调用方法increment_login_attempts() 多次。打印属性login_attempts 的值,确认它被正确地递增;然后,调用方法reset_login_attempts() ,并再次打印属性login_attempts 的值,确认它被重置为0。
code:
class User():
def __init__(self, first_name, second_name, login_attempts):
self.first_name = first_name
self.second_name = second_name
self.login_attempts = login_attempts
def describe(self):
print("This is " + self.first_name + " " + self.second_name)
def greet(self):
print("Hello, I'm " + self.first_name + " " + self.second_name)
def increment_login_attempts(self):
self.login_attempts = self.login_attempts + 1
def reset_login_attempts(self):
self.login_attempts = 0
zero = User('zero', 'Qiu', 0)
print("value of login_attemps" + str(zero.login_attempts))
zero.increment_login_attempts()
print("value of login_attemps" + str(zero.login_attempts))
zero.increment_login_attempts()
print("value of login_attemps" + str(zero.login_attempts))
zero.increment_login_attempts()
print("value of login_attemps" + str(zero.login_attempts))
zero.increment_login_attempts()
print("value of login_attemps" + str(zero.login_attempts))
zero.reset_login_attempts()
print("value of login_attemps" + str(zero.login_attempts))
result:
9-7
题目:管理员
管理员是一种特殊的用户。编写一个名为Admin的类,让它继承你为完成练习9-3或练习9-5而编写的User类。添加一个名为privileges 的属性,用于存储一个由字符串(如"can add post" 、"can delete post" 、"can ban user" 等)组成的列表。编写一个名为show_privileges() 的方法,它显示管理员的权限。创建一个Admin 实例,并调用这个方法。
code:
class User():
def __init__(self, first_name, second_name, login_attempts):
self.first_name = first_name
self.second_name = second_name
self.login_attempts = login_attempts
def describe(self):
print("This is " + self.first_name + " " + self.second_name)
def greet(self):
print("Hello, I'm " + self.first_name + " " + self.second_name)
def increment_login_attempts(self):
self.login_attempts = self.login_attempts + 1
def reset_login_attempts(self):
self.login_attempts = 0
class Admin(User):
def __init__(self, first_name, second_name, login_attempts, privileges):
super().__init__(first_name, second_name, login_attempts)
self.privileges = privileges
def show_privileges(self):
print(self.privileges)
zero = Admin('zero', 'Qiu', 0, "can ban user")
zero.show_privileges()
result: