```python
class IceCream:
def __init__(self, flavor, topping):
self.flavor = flavor
self.topping = topping
def describe(self):
print(f"A {self.flavor} ice cream with {self.topping} topping.")
class IceCreamShop:
def __init__(self, name):
self.name = name
self.menu = {}
def add_to_menu(self, flavor, toppings):
self.menu[flavor] = toppings
def order_ice_cream(self, flavor):
if flavor in self.menu:
ice_cream = IceCream(flavor, self.menu[flavor])
print(f"Preparing your {flavor} ice cream with {self.menu[flavor]} topping...")
ice_cream.describe()
else:
print(f"Sorry, {flavor} is not available in our menu.")
Create an instance of IceCreamShop
my_ice_cream_shop = IceCreamShop("My Ice Cream Shop")
Add items to the menu
my_ice_cream_shop.add_to_menu("Vanilla", "chocolate syrup")
my_ice_cream_shop.add_to_menu("Chocolate", "sprinkles")
my_ice_cream_shop.add_to_menu("Strawberry", "whipped cream")
Place orders
my_ice_cream_shop.order_ice_cream("Vanilla")
my_ice_cream_shop.order_ice_cream("Chocolate")
my_ice_cream_shop.order_ice_cream("Mint")
```
这段代码创建了两个类,IceCream和IceCreamShop。IceCream类表示冰激凌的对象,包含了口味和配料的信息,并且有一个描述冰激凌的方法。IceCreamShop类表示冰激凌店,具有添加菜单和下订单的功能。在示例中,创建了一个IceCreamShop实例,并添加了几种口味的冰激凌到菜单中。模拟了几次订单的过程,包括了从菜单中选择口味,制作冰激凌并描述的过程。
版权声明
本文仅代表作者观点,不代表百度立场。
本文系作者授权百度百家发表,未经许可,不得转载。