9-1
餐馆 : 创建一个名为
Restaurant
的类, 其方法
__init__()
设置两个属性:
restaurant_name
和
cuisine_type
。 创建一个名
为 describe_restaurant() 的方法和一个名为 open_restaurant() 的方法, 其中前者打印前述两项信息, 而后者打印一条消息, 指出餐馆正在营业。
例; 打印有多少人在这家餐馆就餐过, 然后修改这个值并再次打印它。
添加一个名为 set_number_served() 的方法, 它让你能够设置就餐人数。 调用这个方法并向它传递一个值, 然后再次打印这个值。
添加一个名为 increment_number_served() 的方法, 它让你能够将就餐人数递增。 调用这个方法并向它传递一个这样的值: 你认为这家餐馆每天可能接待的就
本的 Restaurant 类都可以, 挑选你更喜欢的那个即可。 添加一个名为 flavors 的属性, 用于存储一个由各种口味的冰淇淋组成的列表。 编写一个显示这些冰淇淋
为 describe_restaurant() 的方法和一个名为 open_restaurant() 的方法, 其中前者打印前述两项信息, 而后者打印一条消息, 指出餐馆正在营业。
根据这个类创建一个名为restaurant 的实例, 分别打印其两个属性, 再调用前述两个方法。
class Restaurant():
def __init__(self,name, type):
self.restaurant_name= name
self.cuisine_type= type
def describle_restaurant(self):
print('this restaurant called ',self.restaurant_name,' sell ',self.cuisine_type)
def open_restaurant(self):
print('this rastaurant is opening! ')
my_restaurant=Restaurant('happymore','chinese_food')
print(my_restaurant.restaurant_name)
print(my_restaurant.cuisine_type)
my_restaurant.describle_restaurant()
my_restaurant.open_restaurant()
输出
happymore
chinese_food
this restaurant called happymore sell chinese_food
this rastaurant is opening!
9-2 三家餐馆 : 根据你为完成练习9-1而编写的类创建三个实例, 并对每个实例调用方法describe_restaurant() 。
class Restaurant():
def __init__(self,name, type):
self.restaurant_name= name
self.cuisine_type= type
def describle_restaurant(self):
print('this restaurant called ',self.restaurant_name,' sell ',self.cuisine_type)
def open_restaurant(self):
print('this rastaurant is opening! ')
my_restaurant=Restaurant('happymore','chinese_food')
my_restaurant.describle_restaurant()
your_restaurant=Restaurant('Nicy','Italy_food')
your_restaurant.describle_restaurant()
her_restaurant=Restaurant('Fly','fast_food')
her_restaurant.describle_restaurant()
输出
this restaurant called happymore sell chinese_food
this restaurant called Nicy sell Italy_food
this restaurant called Fly sell fast_food
9-4
就餐人数 : 在为完成练习
9-1
而编写的程序中, 添加一个名为
number_served
的属性, 并将其默认值设置为
0
。 根据这个类创建一个名为
restaurant
的实例; 打印有多少人在这家餐馆就餐过, 然后修改这个值并再次打印它。
添加一个名为 set_number_served() 的方法, 它让你能够设置就餐人数。 调用这个方法并向它传递一个值, 然后再次打印这个值。
添加一个名为 increment_number_served() 的方法, 它让你能够将就餐人数递增。 调用这个方法并向它传递一个这样的值: 你认为这家餐馆每天可能接待的就
餐人数。
class Restaurant():
def __init__(self,name, type,num=0):
self.restaurant_name= name
self.cuisine_type= type
self.guest_number=num
def print_num(self):
print('there are ',self.guest_number,' people eating dinner. ')
def set_number_served(self,num):
self.guest_number=num
def increment_number_served(num):
self.guest_number += num
my_restaurant=Restaurant('happymore','chinese_food',5)
my_restaurant.print_num()
my_restaurant.set_number_served(10)
my_restaurant.print_num()
输出
there are 5 people eating dinner.
there are 10 people eating dinner.
9-6
冰淇淋小店 : 冰淇淋小店是一种特殊的餐馆。 编写一个名为
IceCreamStand
的类, 让它继承你为完成练习
9-1
或练习
9-4
而编写的
Restaurant
类。 这两个版本的 Restaurant 类都可以, 挑选你更喜欢的那个即可。 添加一个名为 flavors 的属性, 用于存储一个由各种口味的冰淇淋组成的列表。 编写一个显示这些冰淇淋
的方法。 创建一个IceCreamStand 实例, 并调用这个方法。
class Restaurant():
def __init__(self,name, type):
self.restaurant_name= name
self.cuisine_type= type
class IcecreamStand(Restaurant):
def __init__(self,name, type,flavors):
super().__init__(name, type)
self.flavor = []
for x in flavors:
self.flavor.append(x)
def print_flavor(self):
print('this IcecreamStand have ',end="")
for x in self.flavor:
print(x,' ',end="")
print('.')
flavors=['chocalate','chese','apple']
my_icecream=IcecreamStand('Best','ice cream',flavors)
my_icecream.print_flavor()
输出
this IcecreamStand have chocalate chese apple .
9-10 导入Restaurant类 :将最新的Restaurant 类存储在一个模块中。在另一个文件中,导入Restaurant 类,创建一个Restaurant 实例,并调用Restaurant 的一个方法,以确认import 语句正确无误。
(restaurant.py)
class Restaurant():
def __init__(self,name, type):
self.restaurant_name= name
self.cuisine_type= type
class IcecreamStand(Restaurant):
def __init__(self,name, type,flavors):
super().__init__(name, type)
self.flavor = []
for x in flavors:
self.flavor.append(x)
def print_flavor(self):
print('this IcecreamStand have ',end="")
for x in self.flavor:
print(x,' ',end="")
print('.')
(IcecreanStand.py)
from restaurant import IcecreamStand
flavors=['chocalate','chese','apple']
my_icecream=IcecreamStand('Best','ice cream',flavors)
my_icecream.print_flavor()
输出
this IcecreamStand have chocalate chese apple .