本文主要是介绍Python编程从入门到实践 9-6冰淇淋小店,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
冰淇淋小店是一种特殊的餐馆。编写一个名为IceCreamStand 的类,让它继承你为完成练习9-1或练习9-4而编写的Restaurant 类。这两个版 本的Restaurant 类都可以,挑选你更喜欢的那个即可。添加一个名为flavors 的属性,用于存储一个由各种口味的冰淇淋组成的列表。编写一个显示这些冰淇淋 的方法。创建一个IceCreamStand 实例,并调用这个方法。
#!/iser/bin/env python
# coding:utf-8
class Restaurant(object):def __init__(self,restaurant_name,cuisine_type):self.restaurant_name=restaurant_nameself.cuisine_type=cuisine_typeself.number_served=0self.number_serve=0def describe_restaurant(self):#描述餐厅信息print("The restaurant's name is "+self.restaurant_name+", and it's type is "+self.cuisine_type+".")def open_restaurant(self):#描述餐厅状态print("It is opening!")def read_restaurant(self):print("Now,this restaurant has "+str(self.number_served)+" people in it.")def increment_number_served(self,add):#就餐最多人数self.number_serve+=addprint("It's capacity is "+str(add)+".")def set_number_served(self,numbers):#设置就餐人数if numbers>=self.number_served:self.number_served=numbersif numbers>=self.number_serve:print("The restaurant is full!")else:print("The restaurant is empty!")
class IceCreamStand(Restaurant):def __init__(self,restaurant_name,cuisine_type):super(IceCreamStand,self).__init__(restaurant_name,cuisine_type)self.flavors=['chocolate','Strawberry','Original']def describe_ice(self):print("The restaurant has many ice-cream:")for flavor in self.flavors:print(flavor)restaurant=Restaurant('KFC','fast food')
restaurant.describe_restaurant()
restaurant.open_restaurant()
restaurant.increment_number_served(100)
restaurant.set_number_served(40)
restaurant.read_restaurant()ice_cream=IceCreamStand('HunberKing','fast food')
ice_cream.describe_ice()
结果为:
这篇关于Python编程从入门到实践 9-6冰淇淋小店的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!