9.1 餐馆:
class Restaurant():
def __init__(self, restaurant_name, cuisine_type):
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
def describe_restaurant(self):
print("The restaurant is called "+self.restaurant_name)
print("The type of the restaurant's cuisine is "+self.cuisine_type)
def open_restaurant(self):
print("The restaurant '"+self.restaurant_name+"' is opening")
my_restaurant = Restaurant("Xianmiao", "Roast Chiken")
print(my_restaurant.restaurant_name+' '+my_restaurant.cuisine_type)
my_restaurant.describe_restaurant()
my_restaurant.open_restaurant()
执行结果:
Xianmiao Roast Chiken
The restaurant is called Xianmiao
The type of the restaurant's cuisine is Roast Chiken
The restaurant 'Xianmiao' is opening
9-4 就餐人数:
class Restaurant():
def __init__(self, restaurant_name, cuisine_type, number_served=0):
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
self.number_served = number_served
def describe_restaurant(self):
print("The restaurant is called "+self.restaurant_name)
print("The type of the restaurant's cuisine is "+self.cuisine_type)
def open_restaurant(self):
print("The restaurant '"+self.restaurant_name+"' is opening")
my_restaurant = Restaurant("Xianmiao", "Roast Chiken")
print(my_restaurant.number_served)
my_restaurant.number_served = 10
print(my_restaurant.number_served)
执行结果:
0
10
9-6 冰激凌小店:
class Restaurant():
def __init__(self, restaurant_name, cuisine_type, number_served=0):
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
self.number_served = number_served
def describe_restaurant(self):
print("The restaurant is called "+self.restaurant_name)
print("The type of the restaurant's cuisine is "+self.cuisine_type)
def open_restaurant(self):
print("The restaurant '"+self.restaurant_name+"' is opening")
class IceCreamStand(Restaurant):
def __init__(self, restaurant_name, cuisine_type, flavors, number_served=0):
super().__init__(restaurant_name, cuisine_type, number_served)
self.flavors = flavors
def print_flavors(self):
print("This IceCreamStand has such flavors:")
for flavor in self.flavors:
print(flavor, end=' ')
flavors = ["Strawberry", "Chocolates", "Taro"]
my_IceCreamStand = IceCreamStand("Yidiandian", "iceCream", flavors)
my_IceCreamStand.print_flavors()
执行结果:
This IceCreamStand has such flavors:
Strawberry Chocolates Taro
9-14 骰子:
from random import randint
class Die():
def __init__(self, surface):
self.surface = surface
def roll_die(self, times):
i = 1;
while(i<=times):
x = randint(1, self.surface)
print(x, end=' ')
i = i+1
print("")
Die_6 = Die(6)
Die_6.roll_die(10)
Die_10 = Die(10)
Die_10.roll_die(10)
Die_20 = Die(20)
Die_20.roll_die(10)
执行结果:
2 3 1 5 3 1 1 3 1 6
1 10 8 8 5 5 1 1 9 9
10 3 11 15 11 15 8 14 2 8