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

【Python 3.7】冰淇淋小店:冰淇淋小店是一种特殊的餐馆。编写一个名为 IceCreamStand 的类,让它继承你为完成练习 9-1 或练习 9-4 而编写的 Restaurant 类。这两个版本的Restaurant 类都可以,挑选你更喜欢的那个即可。添加一个名为 flavors 的属性,用于存储一个由各种口味的冰淇淋组成的列表。编写一个显示这些冰淇淋的方法。创建一个IceCreamStand 实例,并调用这个方法。

程序为:

class Restaurant():
    def __init__(self, restaurant_name, cuisine_type):
        """初始化属性restaurant_name和cuisine_type"""
        self.restaurant_name = restaurant_name
        self.cuisine_type = cuisine_type
        self.number_served=0

    def describe_restaurant(self):
        print("The restarurant name is " + self.restaurant_name.title() + ".")
        print("The restarurant's cuisine type is " + self.cuisine_type.title() + ".")

    def open_restaurant(self):
        print("The " + self.restaurant_name.title() + " is openning!")

    def served_number(self):
        """打印一条用餐人数的消息"""
        print(str(self.number_served)+" person went to the restaurant.")

    def set_number_served(self,n):
        """将用餐人数设为指定的值"""
        self.number_served = n

    def increment_number_served(self,n):
        """将用餐人数增加指定的量"""
        self.number_served += n
        return self.number_served
class IceCreamStand(Restaurant):
    """冰激凌店的独特之处
    初始化父类的属性,再初始化冰激凌店特有的属性
    """
    def __init__(self, restaurant_name, cuisine_type):
        super().__init__( restaurant_name, cuisine_type)
        self.flavors=['Oreo','milk','banana']

    def describe_flavors(self):
        """打印关于口味的消息"""
        for flavor in self.flavors:
            print("The flavor is " + flavor+".")

my_icecream=IceCreamStand("DQ","fastfood")

my_icecream.describe_flavors()

结果为:

The flavor is Oreo.
The flavor is milk.
The flavor is banana.

你可能感兴趣的:(学习,python,习题,类,父类,子类,入门)