9-1
class Restaurant():
def __init__(self,name,cuisine):
self.restaurant_name = name
self.cuisine_type = cuisine
def describe_restaurant(self):
print("restaurant name:" + self.restaurant_name.title())
print("cuisine type:" + self.cuisine_type.title())
def open_restaurant(self):
print(self.restaurant_name + " is opened.")
restaurant1 = Restaurant("happy maitian","hamburger")
print(restaurant1.restaurant_name + " " + restaurant1.cuisine_type)
restaurant1.describe_restaurant()
restaurant1.open_restaurant()
输出结果:
happy maitian hamburger
restaurant name:Happy Maitian
cuisine type:Hamburger
happy maitian is opened.
9-2
restaurant1 = Restaurant("happy maitian","hamburger")
restaurant2 = Restaurant("wolaizichaozhou","changfen")
restaurant3 = Restaurant("xiaochaoji","chaofan")
restaurant1.describe_restaurant()
restaurant2.describe_restaurant()
restaurant3.describe_restaurant()
输出结果:
restaurant name:Happy Maitian
cuisine type:Hamburger
restaurant name:Wolaizichaozhou
cuisine type:Changfen
restaurant name:Xiaochaoji
cuisine type:Chaofan
9-3
class Restaurant():
def __init__(self,name,cuisine,number = 1):
self.restaurant_name = name
self.cuisine_type = cuisine
self.number_served = number
def describe_restaurant(self):
print("restaurant name:" + self.restaurant_name.title())
print("cuisine type:" + self.cuisine_type.title())
def open_restaurant(self):
print(self.restaurant_name + " is opened.")
def set_number_served(self,number):
self.number_served = number
def increment_number_served(self,number):
self.number_served += number
restaurant = Restaurant("KFC","hamburger")
print("number_served: " + str(restaurant.number_served) + ".")
restaurant.set_number_served(10)
print("number_served: " + str(restaurant.number_served) + ".")
restaurant.increment_number_served(5)
print("number_served: " + str(restaurant.number_served) + ".")
输出结果:
number_served: 1.
number_served: 10.
number_served: 15.
9-6
class IceCreamStand(Restaurant):
def __init__(self,name,cuisine,number,flavors):
super().__init__(name,cuisine,number)
self.flavors = flavors
def show_flavors(self):
print("All flavors of icrcream is followed:")
for flavor in self.flavors:
print(flavor)
IceCreamStand1 = IceCreamStand("Happy","icecream",100,["milk","strawberry","Mango"])
IceCreamStand1.show_flavors()
输出结果:
All flavors of icrcream is followed:
milk
strawberry
Mango
9-10
restaurant.py
class Restaurant():
def __init__(self,name,cuisine,number = 1):
self.restaurant_name = name
self.cuisine_type = cuisine
self.number_served = number
def describe_restaurant(self):
print("restaurant name:" + self.restaurant_name.title())
print("cuisine type:" + self.cuisine_type.title())
def open_restaurant(self):
print(self.restaurant_name + " is opened.")
def set_number_served(self,number):
self.number_served = number
def increment_number_served(self,number):
self.number_served += number
main.py
import Restaurant from restaurant
restaurant = Restaurant("KFC","hamburger",10000)
restaurant.describe_restaurant()
restaurant.open_restaurant()
print("served number: " + str(restaurant.number_served) + ".")
输出结果:
restaurant name:Kfc
cuisine type:Hamburger
KFC is opened.
served number: 10000.