Python教程

Python编程从入门到实践第九章练习二

本文主要是介绍Python编程从入门到实践第九章练习二,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

练习9-6

# 冰淇淋小店:编写一个名为IceCreamStand的类,让它继承case9-1或case9-4编写的Restaurant类。添加一个名为flavors的属性,用于存储一个由各种口味的冰淇淋组成的列表。编写一个显示这些冰淇淋的方法。创建一个IceCreamStand的实例,并调用这个方法。

 1 class Restaurant:
 2     """餐馆营业消息"""
 3 
 4     def __init__(self,restaurant_name,cuisine_type):     # 初始化定义属性
 5         """初始化描述餐馆信息"""
 6         self.restaurant_name = restaurant_name
 7         self.cuisine_type = cuisine_type
 8         self.number_served = 0
 9 
10     def describe_restaurant(self):     # 描打印餐馆概要信息
11         """描述餐馆"""
12         print(f"\nRestaurant name:{self.restaurant_name}")
13         print(f"Cuisine type:{self.cuisine_type}")
14 
15     def open_restaurant(self):     # 表明餐厅正在营业
16         """餐厅营业"""
17         print("\nRestaurant is serving now!")
18 
19     def read_served_number(self):      # 打印就餐人数
20         """打印就餐人数"""
21         print(f"{self.number_served} people have been eaten in this restaurant.")
22 
23     def set_number_served(self,number):     # 设置就餐人数
24         """设置就餐人数"""
25         self.number_served = number
26 
27     def increment_number_served(self,increment):
28         """增加就餐人数"""
29         self.number_served += increment
30 
31 class IceCreamStand(Restaurant):
32     """冰淇淋信息"""
33 
34     def __init__(self,restaurant_name,cuisine_type):
35         """初始化父类的属性"""
36         super().__init__(restaurant_name,cuisine_type)
37         self.flavors = ['vanilla','chocolate','strawberry','cookie dough']
38 
39     def describe_icecream(self):
40         """描述冰淇淋的口味"""
41         print(f"\nThe ice cream in our restraurant are followings:")
42         for self.flavor in self.flavors:
43             print(self.flavor)
44 
45 restaurant = Restaurant('dejav','Hunan cuisine')     #根据类Restaurant创建名为restaurant的实例
46 restaurant.describe_restaurant()   
47 icecreamstand = IceCreamStand('dejav','Hunan cuisine')
48 icecreamstand.describe_icecream()

 

这篇关于Python编程从入门到实践第九章练习二的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!