继承例题
9-6 冰淇淋小店 :冰淇淋小店是一种特殊的餐馆。编写一个名为IceCreamStand 的类,让它继承你为完成练习9-1或练习9-4而编写的Restaurant 类。这两个版 本的Restaurant 类都可以,挑选你更喜欢的那个即可。添加一个名为flavors 的属性,用于存储一个由各种口味的冰淇淋组成的列表。编写一个显示这些冰淇淋 的方法。创建一个IceCreamStand 实例,并调用这个方法。
class Restaurant():
def __init__(self,restaurant_name,cuisine_type):
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
def desc_restaurant(self):
print('the name is ' + self.restaurant_name)
print('the cuisine_type is ' + self.cuisine_type)
def open_restaurant(self):
print('the'+ self.restaurant_name + 'is working')
class IceCreamStand(Restaurant):
def __init__(self,restaurant_name,cuisine_type='ice_cream'):
#里面的参数是表示i调用的时候必须输入的参数
super().__init__(restaurant_name,cuisine_type)
self.flavors = []
def display(self):
for flavor in self.flavors:
print(flavor.title())
icecream = IceCreamStand('Tom')
icecream.flavors = ['vanilla','chocolate','balck cherry']
icecream.desc_restaurant()
icecream.display()
#继承的时候一定要把父类和字类写在同一个文件里面,