1. 程式人生 > >《Python 程式設計-從入門到實踐》9-1~8-17

《Python 程式設計-從入門到實踐》9-1~8-17

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("This restaurant's name is "+self.restaurant_name.capitalize())
		print("The type of cuisine is "+self.cuisine_type)

	def open_restaurant(self):
		print("This restaurant is open")
		
restaurant = Restaurant("WcDonald's","fast food")
restaurant.describe_restaurant()
restaurant.open_restaurant()

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("This restaurant's name is "+self.restaurant_name.capitalize())
		print("The type of cuisine is "+self.cuisine_type)

	def open_restaurant(self):
		print("This restaurant is open")
		
	def set_numebr_served(self,new_number):
		self.number_served = new_number
		
	def increment_number_served(self,probable_number):
		self.number_served += 1

restaurant = Restaurant("WcDonald's","fast food")
print(restaurant.number_served)
restaurant.number_served = 500
print(restaurant.number_served)
restaurant.set_numebr_served(600)
print(restaurant.number_served)
restaurant.increment_number_served(700)

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("This restaurant's name is "+self.restaurant_name.capitalize())
		print("The type of cuisine is "+self.cuisine_type)

	def open_restaurant(self):
		print("This restaurant is open")
		
class IceCreamStand(Restaurant):
	def __init__(self, restaurant_name, cuisine_type):
		super().__init__(restaurant_name, cuisine_type)
		self.flavors = ['Vanilla','Chocolate','Strawberry']
	def list_icecream(self):
		print(self.flavors)

icecreamstand = IceCreamStand("Ben's",'ice cream')
icecreamstand.list_icecream()

9-10 匯入Restaurant類:將最新的Restaurant類儲存在一個模組中。在另一個檔案中,匯入Restaurant類,建立一個Restaurant例項,並呼叫Restaurant的一個方法,以確認import語句正確無誤。

from test import Restaurant

restaurant = Restaurant("WcDonald's","fast food")
restaurant.describe_restaurant()