python从入门到实践 第九章习题(高级编程技术 week5-1)
这一节课主要讲的是在python中面向对象编程的方法。
9.1 创建和使用类
注意:self参数表明可以访问实例中的属性。
9-1/2 三家餐馆
class Restaurant():
def __init__(self, restaurant_name, cuisine_type):
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
def describe_restaurant(self):
print('The name of the reataurant is ', self.restaurant_name.title(), '.')
print('The cuisine type of the restaurant is ', self.cuisine_type.title(), '.')
def open_restaurant(self):
print(self.restaurant_name.title(), ' is open!')
restaurant1 = Restaurant('yong he king', 'dinner')
restaurant1.describe_restaurant()
restaurant1.open_restaurant()
restaurant2 = Restaurant('KFC', 'dinner')
restaurant2.describe_restaurant()
restaurant2.open_restaurant()
restaurant3 = Restaurant("McDonald's", 'dinner')
restaurant3.describe_restaurant()
restaurant3.open_restaurant()
9-3
class User():
def __init__(self, first_name, last_name, job):
self.first_name = first_name
self.last_name = last_name
self.job = job
def describe_user(self):
print('This user\'s first name is ', self.first_name.title(), '.')
print('This user\'s last name is ', self.last_name.title(), '.')
print('This user\'s job is ', self.job, '.')
def greet_user(self):
print('Hello, ', self.first_name, '!')
user_1 = User('Jack', 'Wang', 'engineer')
user_1.describe_user()
user_1.greet_user()
user_2 = User('Yong feng', 'Wang', 'engineer')
user_2.describe_user()
user_2.greet_user()
9.2 使用类和实例
- 能够给类的属性指定默认值
- 对类访问属性的方式进行了分析,一般情况下,最好使用方法来访问属性,而不是直接访问属性,这样子能够在访问期间处理更多的事情。
9-5 尝试登陆次数
class User():
def __init__(self, first_name, last_name, job):
self.first_name = first_name
self.last_name = last_name
self.job = job
self.login_attempts = 0
def increment_login_attempts(self):
self.login_attempts = self.login_attempts + 1
def reset_login_attempts(self):
self.login_attempts = 0
def describe_user(self):
print('This user\'s first name is ', self.first_name.title(), '.')
print('This user\'s last name is ', self.last_name.title(), '.')
print('This user\'s job is ', self.job, '.')
def greet_user(self):
print('Hello, ', self.first_name, '!')
user_1 = User('Jack', 'Wang', 'engineer')
user_1.describe_user()
user_1.greet_user()
user_1.increment_login_attempts()
user_1.increment_login_attempts()
user_1.increment_login_attempts()
user_1.increment_login_attempts()
print(user_1.login_attempts)
user_1.reset_login_attempts()
print(user_1.login_attempts)``
9.3 继承
如果要编写的类是另一个现成类的特殊版本,可使用继承。
子类继承了父类的所有属性和方法,同时还可以定义自己的属性和方法。
- 使用父类的构造函数初始化父类,同时定义自己子类的属性
- 可以重写父类的方法(直接使用名称覆盖)
9-6 冰淇淋小店
class Restaurant():
def __init__(self, restaurant_name, cuisine_type):
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
def describe_restaurant(self):
print('The name of the reataurant is ', self.restaurant_name.title(), '.')
print('The cuisine type of the restaurant is ', self.cuisine_type.title(), '.')
def open_restaurant(self):
print(self.restaurant_name.title(), ' is open!')
class IceCreamStand(Restaurant):
def __init__(self, restaurant_name, cuisine_type, flavors):
super().__init__(restaurant_name, cuisine_type)
self.flavors = flavors
def describe_icecreams(self):
for icecream in self.flavors:
print(icecream)
test = IceCreamStand('KFC', 'dinner', ['apple','milk','chocolate'])
test.describe_icecreams()
9-7/8 管理员 权限
class User():
def __init__(self, first_name, last_name, job):
self.first_name = first_name
self.last_name = last_name
self.job = job
self.login_attempts = 0
def increment_login_attempts(self):
self.login_attempts = self.login_attempts + 1
def reset_login_attempts(self):
self.login_attempts = 0
def describe_user(self):
print('This user\'s first name is ', self.first_name.title(), '.')
print('This user\'s last name is ', self.last_name.title(), '.')
print('This user\'s job is ', self.job, '.')
def greet_user(self):
print('Hello, ', self.first_name, '!')
class Admin(User):
def __init__(self, first_name, last_name, job,privileges):
super().__init__(first_name, last_name, job)
self.privileges = privileges
class Privileges():
def __init__(self, privileges):
self.privileges = privileges
def show_privileges(self):
for privilege in self.privileges:
print(privilege)
p = Privileges(['can add post', 'can delete post','can ban user'])
a = Admin('Lily', 'Wang', 'CEO', p)
a.privileges.show_privileges()
9.4 导入类
这一节主要是说明了如何通过将类作为模块中的名称来导入,从而更有效地组织代码。
当导入整个模块的时候,一般使用import module_name
然后使用句点访问法访问模块内的类,这样子能够避免导入的名称与当前代码文件的名称冲突。
当然,如果仅仅导入一个类或几个类,也可以使用from module_name import class_name
来导入指定的类名,这样之后就可以直接使用这个类名不需要句点访问。
9-12
使用了两个文件来分别存放User
类和Admin
类。运行效果如图所示。
注意:admin模块由于依赖user模块,admin模块内部必须加上from user import User
方可正常运行。
9.5 python标准库
这一节主要讲了怎么使用python提供的标准库实现更多的功能。
9-14 骰子
from random import randint
class Die():
def __init__(self, sides = 6):
self.sides = sides
self.cur_side = 6
def roll_die(self):
self.cur_sides = randint(1, self.sides)
print(self.cur_sides)
test1 = Die()
for i in range(1,11):
test1.roll_die()
test2 = Die(10)
for i in range(1,11):
test2.roll_die()
test3 = Die(20)
for i in range(1,11):
test3.roll_die()
9.6 类编码风格
- 类名应采用驼峰命名法。
- 实例名和模块名都采用小写格式,并在单词之间加上下划线。
- 对于每个类,都应紧跟在类定义后面包含一个文档字符串。这种文档字符串简要地描述类的功能,并遵循编写函数的文档字符串时采用的格式约定。每个模块也都应包含一个文档字符串,对其中的类可用于做什么进行描述。
- 可使用空行来组织代码,但不要滥用。在类中,可使用一个空行来分隔方法;而在模块中,可使用两个空行来分隔类。
- 需要同时导入标准库中的模块和你编写的模块时,先编写导入标准库模块的import语句,再添加一个空行,然后编写导入你自己编写的模块的import语句。在包含多条import语句的程序中,这种做法让人更容易明白程序使用的各个模块都来自何方。