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 self.number_served = 0 # 添加属性,设置默认值为0 def describle_restaurant(self): print(self.restaurant_name, self.cuisine_type) def open_restaurant(self): print("Now is opening...") def read_restaurant(self): print("The restaurant have " + str(self.number_served) + ' people eating.') def set_number_served(self, member): # 添加方法,设置就餐人数 self.number_served = member def increment_number_served(self, members): # 添加方法,设置递增人数 self.number_served += members class IceCreamStand(Restaurant): def __init__(self, restaurant_name, cuisine_type): super().__init__(restaurant_name, cuisine_type) self.flavors = [] def ShowIceCream(self): for flavor in self.flavors: print(flavor) ICS = IceCreamStand('Haagen-Dazs', 'IceCream') ICS.flavors = ['TiLaMiSu', 'AiQingHaiZhiZhou', 'HeiSenLinLianQu'] ICS.ShowIceCream()
3. 执行结果
D:\python编程:从入门到实践\venv\Scripts\python.exe "D:/python编程:从入门到实践/9-6 冰淇淋小店.py"
TiLaMiSu
AiQingHaiZhiZhou
HeiSenLinLianQu
Process finished with exit code 0