9-1 餐馆
class Restaurant():
"""一次模拟餐馆的简单尝试"""
def __init__(self, restaurant_name, cuisine_type):
"""初始化属性restaurant_name和cuisine_type"""
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
def describe_restaurant(self):
"""打印两项信息"""
print("The restaurant's name is " + self.restaurant_name + ".")
print("The cuisine type of the restaurant is " + self.cuisine_type + ".")
def open_restaurant(self):
"""打印一条消息指出餐馆正在营业"""
print("The restaurant is open.")
my_restaurant = Restaurant('Go Believe', 'steamed stuffed bun')
print(my_restaurant.restaurant_name, "\t", my_restaurant.cuisine_type)
my_restaurant.describe_restaurant()
my_restaurant.open_restaurant()
9-2 三家餐馆
class Restaurant():
"""一次模拟餐馆的简单尝试"""
def __init__(self, restaurant_name, cuisine_type):
"""初始化属性restaurant_name和cuisine_type"""
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
def describe_restaurant(self):
"""打印两项信息"""
print("The restaurant's name is " + self.restaurant_name + ".")
print("The cuisine type of the restaurant is " + self.cuisine_type + ".")
def open_restaurant(self):
"""打印一条消息指出餐馆正在营业"""
print("The restaurant is open.")
my_restaurant = Restaurant('Go Believe', 'steamed stuffed bun')
his_restaurant = Restaurant('KFC', 'fast food')
her_restaurant = Restaurant("Mcdonald's", 'fast food')
my_restaurant.describe_restaurant()
his_restaurant.describe_restaurant()
her_restaurant.describe_restaurant()
9-3 用户
class User():
"""用户简介"""
def __init__(self, first_name, last_name, age, agenda):
self.first_name = first_name
self.last_name = last_name
self.age = age
self.agenda = agenda
def describe_user(self):
"""打印用户的信息摘要"""
print('User: ' + self.first_name + ' ' + self.last_name)
print('Age: ' + str(self.age))
print('Agenda: ' + self.agenda + '\n')
def greet_user(self):
"""向用户发出个性化的问候"""
print('Welcome ' + self.first_name.title() + '!\n')
first_user = User('allan', 'poe', 13, 'male')
second_user = User('niels', 'bohr', 18, 'male')
third_user = User('marie', 'curie', 23, 'female')
first_user.describe_user()
first_user.greet_user()
second_user.describe_user()
second_user.greet_user()
third_user.describe_user()
third_user.greet_user()