7.类的习题
7.1.创建一个restaurant类,初始化语句设置 name 和 type 两个属性,定义open 和 describe 连个方法。
打印 开门 和 餐厅的概况。
class Restaurant:
def __init__(self, restaurant_name, cuisine_type):
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
def open_restaurant(self):
print(f"The {self.restaurant_name}-restaurant is open now . ")
def describe_restaurant(self):
print(f"The name of this restaurant is {self.restaurant_name} .")
print(f"We mainly cook {self.cuisine_type} .")
7.2.实例化以上的类,调用类的方法
my_1st_restaurant = Restaurant('he shan zhai', 'chinese cuisine')
my_1st_restaurant.describe_restaurant()
my_1st_restaurant.open_restaurant()
my_2ed_restaurant = Restaurant('tai yuan', 'Thailand cuisine')
my_2ed_restaurant.describe_restaurant()
my_3rd_restaurant = Restaurant('liu he hui zai guan', 'hui cuisine')
my_3rd_restaurant.describe_restaurant()
The name of this restaurant is he shan zhai .
We mainly cook chinese cuisine .
The he shan zhai-restaurant is open now .
The name of this restaurant is tai yuan .
We mainly cook Thailand cuisine .
The name of this restaurant is liu he hui zai guan .
We mainly cook hui cuisine .
7.3 创建user类,包含frist_name last_name两项属性,定义describe 方法描述user信息,定义greet 方法 ,向user打招呼。
class User:
def __init__(self, frist_name, last_name, age, **kwargs):
self.frist_name = frist_name
self.last_name = last_name
self.age = age
self.dict = {}
for k, v in kwargs.items():
setattr(self, k, v)
self.dict[k] = v
# print(self.dict)
def full_name(self):
full_name = f"{self.last_name} {self.frist_name}".title()
return full_name
def describe_user(self):
print(f"The user's name is {self.full_name()} .")
print(f"He/She is {self.age} years old.")
print(f"{self.full_name()}'s other information:"
f"\n\t{self.dict}")
def greet_user(self):
print(f"Hello , {self.full_name()} !")
user_alise = User('alise', 'homefese', 19, hight='177', job='police', nation='America')
user_alise.describe_user()
user_li = User('wenlong', 'li', 28, hight='181', college='Wu han university', nation='china')
user_li.describe_user()
user_li.greet_user()
user_alise.greet_user()
print(user_li.nation)
The user's name is Homefese Alise .
He/She is 19 years old.
Homefese Alise's other information:
{'hight': '177', 'job': 'police', 'nation': 'America'}
The user's name is Li Wenlong .
He/She is 28 years old.
Li Wenlong's other information:
{'hight': '181', 'college': 'Wu han university', 'nation': 'china'}
Hello , Li Wenlong !
Hello , Homefese Alise !
china
7-4. 在9-1中创建的类中,添加一个number_served属性,并将其默认值设为0.再添加一个名为set_number_served方法,使他可以传入参数 number_served;再添加一个increment_number_served方法,让就餐人数可以递增。调用两个方法实现其功能。
class Restaurant2:
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(f"The name of this restaurant is {self.restaurant_name.title()}.")
print(f"We mainly serves {self.cuisine_type}.")
def set_number_served(self, number_served):
self.number_served = number_served
print(f"We serve {self.number_served} people today.")
def increament_number_served(self, increament_number):
self.number_served += increament_number
print(f"We serve {self.number_served} people today.")
my_4th_restaurant = Restaurant2('fu gui yuan', 'shandong cuisine')
my_4th_restaurant.describe_restaurant()
my_4th_restaurant.set_number_served(90)
my_4th_restaurant.increament_number_served(99)
The name of this restaurant is Fu Gui Yuan.
We mainly serves shandong cuisine.
We serve 90 people today.
We serve 189 people today.