9-1 餐馆 :创建一个名为Restaurant 的类,其方法__init__() 设置两个属性:restaurant_name 和cuisine_type 。创建一个名为describe_restaurant() 的方法和一个名为open_restaurant() 的方法,其中前者打印前述两项信息,而后者打印一条消息,指出餐馆正在营业。

根据这个类创建一个名为restaurant 的实例,分别打印其两个属性,再调用前述两个方法。

代码:

class Restaurant:
	def __init__(self,restaurant_name, cuisine_type):
		self.restaurant_name=restaurant_name
		self.cuisine_type=cuisine_type

	def describe_restaurant(self):
		print("Restaurant_name: " + self.restaurant_name.title())
		print("Cuisine_type: " + self.cuisine_type.title())

	def open_restaurant(self):
		print("The restaurant is open.")

my_restaurant=Restaurant("chinese","chinese style")
my_restaurant.describe_restaurant()
my_restaurant.open_restaurant()

结果:

Restaurant_name: Chinese
Cuisine_type: Chinese Style
The restaurant is open.



9-2 三家餐馆 :根据你为完成练习9-1而编写的类创建三个实例,并对每个实例调用方法describe_restaurant() 。

代码:

class Restaurant:
	def __init__(self,restaurant_name, cuisine_type):
		self.restaurant_name=restaurant_name
		self.cuisine_type=cuisine_type

	def describe_restaurant(self):
		print("\nRestaurant_name: " + self.restaurant_name.title())
		print("Cuisine_type: " + self.cuisine_type.title())

	def open_restaurant(self):
		print("The restaurant is open.")

my_restaurant=Restaurant("chinese","chinese style")
my_restaurant.describe_restaurant()

my_restaurant1=Restaurant("happy time","endlish style")
my_restaurant1.describe_restaurant()

my_restaurant2=Restaurant("eating","indian style")
my_restaurant2.describe_restaurant()

结果:

Restaurant_name: Chinese
Cuisine_type: Chinese Style

Restaurant_name: Happy Time
Cuisine_type: Endlish Style

Restaurant_name: Eating
Cuisine_type: Indian Style



9-3 用户 :创建一个名为User 的类,其中包含属性first_name 和last_name ,还有用户简介通常会存储的其他几个属性。在类User 中定义一个名为describe_user() 的方法,它打印用户信息摘要;再定义一个名为greet_user() 的方法,它向用户发出个性化的问候。

创建多个表示不同用户的实例,并对每个实例都调用上述两个方法。

代码:

class user:
	def __init__(self,first_name,last_name):
		self.first_name=first_name
		self.last_name=last_name

	def describe_user(self):
		print("User'name: "+ self.last_name.title()+" "+ self.first_name.title())

	def greet_user(self):
		print("Hello, " + self.last_name.title()+" "+ self.first_name.title()+"!")

user_1=user('geroge',"tim")
user_1.describe_user()
user_1.greet_user()
print("\n")

user_2=user('while',"tom")
user_2.describe_user()
user_2.greet_user()
print("\n")

user_3=user('li',"ming")
user_3.describe_user()
user_3.greet_user()

结果:

User'name: Tim Geroge
Hello, Tim Geroge!


User'name: Tom While
Hello, Tom While!


User'name: Ming Li
Hello, Ming Li!





9-4 就餐人数 :在为完成练习9-1而编写的程序中,添加一个名为number_served 的属性,并将其默认值设置为0。根据这个类创建一个名为restaurant 的实例;打印有多少人在这家餐馆就餐过,然后修改这个值并再次打印它。

添加一个名为set_number_served() 的方法,它让你能够设置就餐人数。调用这个方法并向它传递一个值,然后再次打印这个值。添加一个名为increment_number_served() 的方法,它让你能够将就餐人数递增。调用这个方法并向它传递一个这样的值:你认为这家餐馆每天可能接待的就餐人数。 

代码:



class Restaurant:
	def __init__(self,restaurant_name, cuisine_type):
		self.restaurant_name=restaurant_name
		self.cuisine_type=cuisine_type
		self.number_served=0;
	def describe_restaurant(self):
		print("Restaurant_name: " + self.restaurant_name.title())
		print("Cuisine_type: " + self.cuisine_type.title())
	def get_number_served(self):
		print("The restaurant has served for "+ str(self.number_served) + ".")
	def open_restaurant(self):
		print("The restaurant is open.")
	def set_number_served(self,number):
		self.number_served=number
	def increment_number_served(self,increment):
		self.number_served += increment

my_restaurant1=Restaurant("happy time","endlish style")
my_restaurant1.describe_restaurant()
my_restaurant1.get_number_served()
my_restaurant1.set_number_served(98)
my_restaurant1.get_number_served()
my_restaurant1.increment_number_served(2)
my_restaurant1.get_number_served()

结果:

Restaurant_name: Happy Time
Cuisine_type: Endlish Style
The restaurant has served for 0.
The restaurant has served for 98.
The restaurant has served for 100.




9-6 冰淇淋小店 :冰淇淋小店是一种特殊的餐馆。编写一个名为IceCreamStand 的类,让它继承你为完成练习9-1或练习9-4而编写的Restaurant 类。这两个版本的Restaurant 类都可以,挑选你更喜欢的那个即可。添加一个名为flavors 的属性,用于存储一个由各种口味的冰淇淋组成的列表。编写一个显示这些冰淇淋的方法。创建一个IceCreamStand 实例,并调用这个方法。 

代码:


class Restaurant:
	def __init__(self,restaurant_name, cuisine_type):
		self.restaurant_name=restaurant_name
		self.cuisine_type=cuisine_type
		self.number_served=0;
	def describe_restaurant(self):
		print("Restaurant_name: " + self.restaurant_name.title())
		print("Cuisine_type: " + self.cuisine_type.title())
	def get_number_served(self):
		print("The restaurant has served for "+ str(self.number_served) + ".")
	def open_restaurant(self):
		print("The restaurant is open.")
	def set_number_served(self,number):
		self.number_served=number
	def increment_number_served(self,increment):
		self.number_served += increment

class IceCreamStand(Restaurant):
	def __init__(self,restaurant_name, cuisine_type) :
		super().__init__(restaurant_name, cuisine_type)
		self.flavors = []
	def add_flavors(self,kind):
		self.flavors.append(kind)
	def get_flavors(self):
		print(self.flavors)

my_IceCreamStand=IceCreamStand("chinese","chinese style")
my_IceCreamStand.add_flavors("mango")
my_IceCreamStand.add_flavors("apple")
my_IceCreamStand.add_flavors("pineapple")
my_IceCreamStand.get_flavors()

结果:

['mango', 'apple', 'pineapple']




9-10 导入Restaurant类 :将最新的Restaurant 类存储在一个模块中。在另一个文件中,导入Restaurant 类,创建一个Restaurant 实例,并调用Restaurant 的一个方法,以确认import 语句正确无误。 

代码:


(restaurant.py)

class Restaurant:
	def __init__(self,restaurant_name, cuisine_type):
		self.restaurant_name=restaurant_name
		self.cuisine_type=cuisine_type
		self.number_served=0;
	def describe_restaurant(self):
		print("Restaurant_name: " + self.restaurant_name.title())
		print("Cuisine_type: " + self.cuisine_type.title())
	def get_number_served(self):
		print("The restaurant has served for "+ str(self.number_served) + ".")
	def open_restaurant(self):
		print("The restaurant is open.")
	def set_number_served(self,number):
		self.number_served=number
	def increment_number_served(self,increment):
		self.number_served += increment

(my_restaurant.py)


from restaurant import Restaurant

my_rerestaurant_1 =  Restaurant("chinese","style")
my_rerestaurant_1.describe_restaurant()

结果:

Restaurant_name: Chinese
Cuisine_type: Style




9-14 骰子 :模块random 包含以各种方式生成随机数的函数,其中的randint() 返回一个位于指定范围内的整数,例如,下面的代码返回一个1~6内的整数:

请创建一个Die 类,它包含一个名为sides 的属性,该属性的默认值为6。编写一个名为roll_die() 的方法,它打印位于1和骰子面数之间的随机数。创建一个6面的骰子,再掷10次。 创建一个10面的骰子和一个20面的骰子,并将它们都掷10次。

代码:

from random import randint

class Die:
	def __init__(self):
		sides=6

	def set_sides(self,sides_):
		self.sides=sides_

	def roll_die(self):
		for a in range(0,10):
			x = randint(1, self.sides)
			print(x)

My_die = Die()
My_die.set_sides(6)
My_die.roll_die()
print("\n")

My_die1 = Die()
My_die1.set_sides(10)
My_die1.roll_die()
print("\n")

My_die2 = Die()
My_die2.set_sides(20)
My_die2.roll_die()

结果:


5
2
2
3
2
2
3
6
5
4


1
4
6
5
3
2
9
9
9
7


14
8
4
14
20
16
1
20
1
5