9-6 冰淇淋小店

1. 项目

冰淇淋小店是一种特殊的餐馆。编写一个名为 IceCreamStand 类,让它继承你为完成练习 9-1 或练习 9-4 而编写的 Restaurant 类。

这两个版本的Restaurant 类都可以,挑选你更喜欢的那个即可。添加一个名为 flavors 的属性,用于存储一个由各种口味的冰淇淋组成的列表。

编写一个显示这些冰淇淋的方法。创建一个IceCreamStand 实例,并调用这个方法。

 

2. 代码

class Restaurant():
    def __init__(self, restaurant_name, cuisine_type):
        self.restaurant_name = restaurant_name
        self.cuisine_type = cuisine_type

    def describe_restaurant(self):
        print("The " + self.restaurant_name + " have " +
              str(self.cuisine_type) + " kinds of food.")

    def open_restaurant(self):
        print("Now is opening.")

"""创建flavors列表用于存储各种冰淇淋列表"""
flavors = ('a', 'b', 'c')
class IceCreamStand(Restaurant):
    """初始化子类属性"""
    def __init__(self, restaurant_name, cuisine_type):
        """继承父类属性"""
        super().__init__(restaurant_name, cuisine_type)
        """添加flavors属性"""
        self.flavors = flavors

    def display_ICS(self):
        """显示冰淇淋的方法"""
        print(self.flavors)

"""创建实例化"""
ics = IceCreamStand('KFC', 100)
"""调用方法"""
ics.display_ICS()

  

3. 执行结果

('a', 'b', 'c')

  

posted @ 2021-06-27 21:57  JRS077  阅读(125)  评论(0编辑  收藏  举报