第五周第一次作业

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 ('the restaurant is named as ' + self.restaurant_name)
		print ("it's cuisine is " + self.cuisine_type)
	
	def open_restaurant(self):
		print ('the restaurant is open now')
		

restaurant = Restaurant('wangzhihe', 'Chinese')
restaurant.describe_restaurant()
restaurant.open_restaurant()
结果:
the restaurant is named as wangzhihe
it's cuisine is Chinese
the restaurant is open now
9-4 就餐人数 :在为完成练习9-1而编写的程序中,添加一个名为number_served 的属性,并将其默认值设置为0。根据这个类创建一个名为restaurant 的实例;打印有多少人在这家餐馆就餐过,然后修改这个值并再次打印它。
添加一个名为set_number_served() 的方法,它让你能够设置就餐人数。调用这个方法并向它传递一个值,然后再次打印这个值。

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

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 named as ' + self.restaurant_name)
		print ("it's cuisine is " + self.cuisine_type)
	
	def open_restaurant(self):
		print ('the restaurant is open now')
		
	def set_number_served(self, number_served):
		self.number_served = number_served
	
	def increment_number_served(self, increment):
		self.number_served += increment
	
	
restaurant = Restaurant('wangzhihe', 'Chinese')
print ('there are ' + str(restaurant.number_served) + ' people served here')
restaurant.set_number_served(50)
print ('there are ' + str(restaurant.number_served) + ' people served here')
restaurant.increment_number_served(200)
print ('there are ' + str(restaurant.number_served) + ' people served here')
结果:
there are 0 people served here
there are 50 people served here
there are 250 people served here


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
		
	def describe_restaurant(self):
		print ('the restaurant is named as ' + self.restaurant_name)
		print ("it's cuisine is " + self.cuisine_type)
	
	def open_restaurant(self):
		print ('the restaurant is open now')
		
	
class IceCreamStand(Restaurant):
	def __init__(self, restaurant_name, cuisine_type, flavors):
		super().__init__(restaurant_name, cuisine_type)
		self.flavors = flavors

	def print_flavors(self):
		print ('flavors:')
		[print (x, end = ', ') for x in self.flavors]

ics = IceCreamStand('ziguangge', 'Chinese', 
					['stinky tofu', 'vanilla', 'chocolate'])
ics.describe_restaurant()
ics.open_restaurant()
ics.print_flavors()
结果:
the restaurant is named as ziguangge
it's cuisine is Chinese
the restaurant is open now
flavors:
stinky tofu, vanilla, chocolate,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值