代码:
#9.1
print(9.1)
class Restaurant():
def __init__(self,name,cuisine_type,number_served=0):
self.name = name
self.cuisine_type = cuisine_type
self.number_served = number_served
def describe_restaurant(self):
print("restaurant name: "+self.name)
print("cuisine type: "+self.cuisine_type)
def open_restaurant(self):
print("The restaurant is open.")
my_restaurant = Restaurant("MFC","Coffee")
my_restaurant.describe_restaurant()
my_restaurant.open_restaurant()
print()
#9.2
print(9.2)
restaurants = {"AAA":"Cantonese","BBB":"Western","CCC":"Seafood"}
info = []
for name,ctype in restaurants.items():
info.append(Restaurant(name,ctype))
for each in info:
each.describe_restaurant()
print()
#9.4
print(9.4)
print(my_restaurant.number_served)
my_restaurant.number_served = 100
print(my_restaurant.number_served)
print()
#9.6
print(9.6)
class IceCreamStand(Restaurant):
def __init__(self,name,cuisine_type,number_served=0):
super().__init__(name,cuisine_type,number_served)
self.flavors = ['apple','banana','melon']
def describe_icecream(self):
i = 1
for each in self.flavors:
print("flavor"+str(i)+": "+each)
i += 1
my_icecreamstand = IceCreamStand(my_restaurant.name,my_restaurant.cuisine_type)
my_icecreamstand.describe_icecream()
my_icecreamstand.describe_restaurant()
输出:
9.1
restaurant name: MFC
cuisine type: Coffee
The restaurant is open.
9.2
restaurant name: AAA
cuisine type: Cantonese
restaurant name: BBB
cuisine type: Western
restaurant name: CCC
cuisine type: Seafood
9.4
0
100
9.6
flavor1: apple
flavor2: banana
flavor3: melon
restaurant name: MFC
cuisine type: Coffee