python编程:从入门到实践 练习 9-6 疑问之处请指教
求大神指点迷津,小弟研一机械工程,目前自学Python
想玩玩嵌入式之类的,有大神指点下更好,十分感谢!!
9-6冰淇淋小店 :
冰淇淋小店是一种特殊的餐馆。 编写一个名为icecreamstand 的类,让它继承你为完成练习9-1或练习9-4而编写的restaurant 类。这两个版本的restaurant 类都可以,挑选你更喜欢的那个即可。添加一个名为flavors 的属性,用于存储一个由各种口味的冰淇淋组成的列表。编写一个显示这些冰淇淋的方法。创建一个icecreamstand 实例,并调用这个方法。
class Restaurant():
def __init__(self,restaurant_name,cuuisine_type):
self.restaurant_name = restaurant_name
self.cuuisine_type = cuuisine_type
def describe_restaurant(self):
print("My restaurant's name is: " + self.restaurant_name.title())
print("My cuuisine_type is: " + self.cuuisine_type)
def open_restaurant(self):
print("Our restaurant is open now.")
class IceCreamStand(Restaurant):
def __init__(self,restaurant_name,cuuisine_type):
super().__init__(restaurant_name,cuuisine_type)
self.flavors = ['strawberry','lemon','matcha']
def Icecream_flavor(self):
print("我们提供以下冰淇淋口味:")
for self.flavor in self.flavors:
print(self.flavor)
IceCreamStand = IceCreamStand("冰淇淋小店","冷饮")
IceCreamStand.Icecream_flavor()
这是运行正确的代码
有疑问的地方是 口味列表[‘strawberry’,‘lemon’,‘matcha’]能不能放在子类外面,然后在子类里添加口味这个属性,如下:
def __init__(self,restaurant_name,cuuisine_type):
super().__init__(restaurant_name,cuuisine_type,flavors)
self.flavors = flavors
def Icecream_flavor(self):
print("我们提供以下冰淇淋口味:")
for self.flavor in self.flavors:
print(self.flavor)
IceCreamStand = IceCreamStand("冰淇淋小店","冷饮",['strawberry','lemon','matcha'])
IceCreamStand.Icecream_flavor()
但是运行结果,,,,,