9-1 餐馆
class Restaurant():
"""the restaurant"""
def __init__(self, restaurant_name, cuisine_type):
"""Initialize the restaurant"""
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
def describe_restaurant(self):
"""Display a summary of the restaurant"""
print("name: " + self.restaurant_name)
print("cuisine type: " + self.cuisine_type)
def open_restaurant(self):
"""the restaurant is open"""
print("The restaurant is open.")
restaurant = Restaurant("Hidilao hot pot", "hot pot")
print(restaurant.restaurant_name)
print(restaurant.cuisine_type)
restaurant.describe_restaurant()
restaurant.open_restaurant()
9-4 就餐人数
class Restaurant():
"""the restaurant"""
def __init__(self, restaurant_name, cuisine_type):
"""Initialize the restaurant"""
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
self.number_served = 0
def describe_restaurant(self):
"""Display a summary of the restaurant"""
print("name: " + self.restaurant_name)
print("cuisine type: " + self.cuisine_type)
print("number served: " + str(self.number_served))
def open_restaurant(self):
"""the restaurant is open"""
print("The restaurant is open.")
def set_number_served(self, new_number):
"""set number served"""
self.number_served = new_number
def increament_number_served(self, additional_served):
"""increament number served"""
self.number_served += additional_served
restaurant = Restaurant("Hidilao hot pot", "hot pot")
print(restaurant.restaurant_name)
print(restaurant.cuisine_type)
print(restaurant.number_served)
restaurant.describe_restaurant()
restaurant.open_restaurant()
restaurant.set_number_served(10)
print(restaurant.number_served)
restaurant.increament_number_served(5)
print(restaurant.number_served)
9-6 冰激凌小店
class Restaurant():
"""the restaurant"""
def __init__(self, restaurant_name, cuisine_type):
"""Initialize the restaurant"""
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
def describe_restaurant(self):
"""Display a summary of the restaurant"""
print("name: " + self.restaurant_name)
print("cuisine type: " + self.cuisine_type)
def open_restaurant(self):
"""the restaurant is open"""
print("The restaurant is open.")
class IceCreamStand(Restaurant):
"""A class representing a restaurant"""
def __init__(self, restaurant_name, cuisine_type = "ice_cream"):
"""Initialize an ice cream stand"""
super().__init__(restaurant_name, cuisine_type)
flavors = []
def show_flavors(self):
"""show flavors"""
for flavor in self.flavors:
print(flavor)
ice_cream_stand = IceCreamStand("ice cream stand")
ice_cream_stand.flavors = ['vanilla', 'chocolate', 'black cherry']
ice_cream_stand.describe_restaurant()
ice_cream_stand.open_restaurant()
ice_cream_stand.show_flavors()