创建一个名为Restaurant 的类,其方法__init__() 设置两个属性:restaurant_name 和cuisine_type。添加一个名为number_served 的属性,并将其默认值设置为0。根据这个类创建一个名为restaurant 的实 例;打印有多少人在这家餐馆就餐过,然后修改这个值并再次打印它。 添加一个名为set_number_served() 的方法,它让你能够设置就餐人数。调用这个方法并向它传递一个值,然后再次打印这个值。 添加一个名为increment_number_served() 的方法,它让你能够将就餐人数递增。调用这个方法并向它传递一个这样的值:你认为这家餐馆每天可能接待的就 餐人数。 。创建一个名 为describe_restaurant() 的方法和一个名为open_restaurant() 的方法,其中前者打印前述两项信息,而后者打印一条消息,指出餐馆正在营业。
时间: 2023-11-27 07:49:00 浏览: 100
以下是代码实现:
```python
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(f"Restaurant name: {self.restaurant_name}")
print(f"Cuisine type: {self.cuisine_type}")
def open_restaurant(self):
print(f"{self.restaurant_name} is now open!")
def set_number_served(self, number):
self.number_served = number
def increment_number_served(self, number):
self.number_served += number
# 创建一个名为restaurant的实例
restaurant = Restaurant("Chinese Restaurant", "Chinese Cuisine")
# 打印有多少人在这家餐馆就餐过
print(f"Number of people served: {restaurant.number_served}")
# 修改就餐人数并再次打印它
restaurant.number_served = 10
print(f"Number of people served: {restaurant.number_served}")
# 调用set_number_served方法并传递一个值
restaurant.set_number_served(20)
print(f"Number of people served: {restaurant.number_served}")
# 调用increment_number_served方法并传递一个值
restaurant.increment_number_served(50)
print(f"Number of people served: {restaurant.number_served}")
# 调用describe_restaurant和open_restaurant方法
restaurant.describe_restaurant()
restaurant.open_restaurant()
```
输出结果:
```
Number of people served: 0
Number of people served: 10
Number of people served: 20
Number of people served: 70
Restaurant name: Chinese Restaurant
Cuisine type: Chinese Cuisine
Chinese Restaurant is now open!
```
阅读全文