9-6冰淇淋小店:冰淇淋小店是一种特殊的餐馆。编写一个名为 IceCreamStand 的类,让它继承你为完成练习 9-1 或练习 9-4 而编写的 Restaurant 类。这两个版本的Restaurant 类都可以,挑选你更喜欢的那个即可。添加一个名为 flavors 的属性,用于存储一个由各种口味的冰淇淋组成的列表。编写一个显示这些冰淇淋的方法。创建一个IceCreamStand 实例,并调用这个方法。
class Restaurant():
def init(self,restaurant_name,cuisine_name):
self.name=restaurant_name
self.cuisine=cuisine_name
self.number_served=0
def describe_restaurant(self):print("这家餐厅名字叫"+self.name+"\n他们做"+self.cuisine)def open_restaurant(self):print("餐厅正在营业")def read(self):print("当前用餐人数"+str(self.number_served))def set_number_served(self,n):'''之前就餐人数'''self.number_served=nprint("已经在本餐厅用餐过的人数为"+str(self.number_served))def increment_number_served(self,number):self.number_served+=numberreturn self.number_served
‘’’
class Icecreamstand(Restaurant):
def __init__(self,restaurant_name,cuisine_name):super().__init__(restaurant_name,cuisine_name)self.flavors='cherry'def fangfa(self):print("The shop's name is "+self.name+".")print("The cuisine they serve is "+self.cuisine)print("The flavor they have is "+self.flavors)
myshop=Icecreamstand(‘bob’,‘icecream’)
myshop.fangfa()
‘’’
#修改后的
class Icecream(Restaurant):
def init(self,restaurant_name,cuisine_name):
super().init(restaurant_name,cuisine_name)
self.flavors=[‘chocolate’,‘mince’,‘cherry’,‘Oreo’,‘milk’]
def describe_flavors(self):
for i in self.flavors:
print("The flavor is "+i+’.’)
my_icecream=Icecream(‘DQ’,‘fasyfood’)
my_icecream.describe_flavors()
注意细节:super().init(属性1,属性2)