冰激凌小店是一种特殊的餐馆。编写 一个名为IceCreamStand 的类,让它继承为完成练习9-1或练 习9-4而编写的Restaurant 类。这两个版本的Restaurant 类 都可以,挑选你更喜欢的那个即可。添加一个名为flavors 的 属性,用于存储一个由各种口味的冰激凌组成的列表。编写一 个显示这些冰激凌的方法。创建一个IceCreamStand 实例, 并调用这个方法
时间: 2024-02-28 12:55:36 浏览: 149
好的,根据您的要求,我为您编写了以下的IceCreamStand类:
```python
class Restaurant:
"""一个表示餐馆的类"""
def __init__(self, restaurant_name, cuisine_type):
"""初始化餐馆的属性"""
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
def describe_restaurant(self):
"""打印餐馆的信息"""
print(f"The restaurant's name is {self.restaurant_name}.")
print(f"The cuisine type is {self.cuisine_type}.")
def open_restaurant(self):
"""模拟餐馆正在营业"""
print(f"{self.restaurant_name} is now open!")
class IceCreamStand(Restaurant):
"""一个表示冰激凌小店的类"""
def __init__(self, restaurant_name, cuisine_type):
"""
初始化冰激凌小店的属性
其中flavors为一个由各种口味的冰激凌组成的列表
"""
super().__init__(restaurant_name, cuisine_type)
self.flavors = ['chocolate', 'vanilla', 'strawberry', 'mint']
def display_flavors(self):
"""打印冰激凌的口味"""
print("We have the following flavors:")
for flavor in self.flavors:
print(f"- {flavor}")
```
您可以通过以下代码创建一个IceCreamStand实例,并调用display_flavors方法:
```python
my_ice_cream_stand = IceCreamStand("Cool Ice Cream", "Ice Cream")
my_ice_cream_stand.display_flavors()
```
这样,程序就会输出以下结果:
```
We have the following flavors:
- chocolate
- vanilla
- strawberry
- mint
```
阅读全文
相关推荐















