【Python3.7】 就餐人数:在为完成练习 9-1 而编写的程序中,添加一个名为 number_served的属性,并将其默认值设置为 0。根据这个类创建一个名为 restaurant 的实例;打印有多少人在这家餐馆就餐过,然后修改这个值并再次打印它。添加一个名为 set_number_served() 的方法,它让你能够设置就餐人数。调用这个方法并向它传递一个值,然后再次打印这个值。添加一个名为 increment_number_served() 的方法,它让你能够将就餐人数递增。调用这个方法并向它传递一个这样的值:你认为这家餐馆每天可能接待的就餐人数。
1.程序为
class Restaurant():
def __init__(self,restaurant_name,cuisine_type):
"""初始化两个属性"""
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
self.number_served = 0
def describe_restaurant(self):
"""餐厅名称和菜肴类型"""
print("The restaurant name is " + self.restaurant_name.title() + ".")
print("The cuisine type is " + self.cuisine_type.title() + ".")
def open_restaurant(self):
"""餐厅正在营业"""
print("The restaurant is openning!")
def read_number_served(self):
"""打印就餐过的人数"""
print(str(self.number_served) + " people have eaten in the restaurant.")
def set_number_served(self,number):
"""指定已经就过餐的人数"""
self.number_served = number
def increment_number_served(self,number):
"""每天可以接待的人数"""
self.number_served += number
2.调用打印函数
my_restaurant = Restaurant('quanjude','chinese food')
my_restaurant.describe_restaurant()
my_restaurant.open_restaurant()
my_restaurant.read_number_served()
my_restaurant.set_number_served(2)
my_restaurant.read_number_served()
my_restaurant.increment_number_served(500)
my_restaurant.read_number_served()
3.输出结果
The restaurant name is Quanjude.
The cuisine type is Chinese Food.
The restaurant is openning!
0 people have eaten in the restaurant.
2 people have eaten in the restaurant.
502 people have eaten in the restaurant.