9-4 就餐人数:在为完成练习9-1而编写的程序中,添加一个名为number_served的属性,并将其默认值设置为0。
根据这个类创建一个名为restaurant的实例;打印有多少人在这家餐馆就餐过,然后修改这个值并再次打印它。
添加一个名为set_number_served()的方法,它让你能够设置就餐人数。调用这个方法并向它传递一个值,然后再次打印这个值。
添加一个名为increment_number_served()的方法,它让你能够将就餐人数递增。调用这个方法并向它传递一个这样的值:你认为这家餐馆每天可能接待的就餐人数。
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("\nThis restaurant's name is " + self.restaurant_name)
print("it's type is " + self.cuisine_type)
def open_restaurant(self):
print('\nThe restaurant is open')
def get_number_served(self):
print('This restaurant has ' + str(self.number_served)+ ' customers everyday.')
def set_number_served(self,number):
self.number_served = number
def increment_number_served(self,num):
self.number_served += num
```实例化
restaurant = Restaurant('xixili','fastfood')
restaurant.number_served = 20
restaurant.get_number_served()
restaurant.set_number_served(50)
restaurant.get_number_served()
restaurant.increment_number_served(20)
restaurant.get_number_served()