python练习——类

1.类的创建与实例化

#9-1

class restaurant():
	def __init__(self,name,cuisine_type):
		self.restaurant_name = name
		self.cuisine_type = cuisine_type
		
	def describe_restaurant(self):
		print(self.restaurant_name + ' is a(an) ' + self.cuisine_type + ' restaurant.\n')
		
	def open_restaurant(self):
		print(self.restaurant_name + ' is now serving.\n')
		
res = restaurant('blue moon','chinese')
res.describe_restaurant()
res.open_restaurant()


2.类元素的修改

#9-3
class restaurant():
	def __init__(self,name,cuisine_type,num = 0 ):
		self.restaurant_name = name
		self.cuisine_type = cuisine_type
		self.number_served = num
		
	def describe_restaurant(self):
		print(self.restaurant_name + ' is a(an) ' + self.cuisine_type + ' restaurant.\n')
		print(self.restaurant_name + ' has served ' + str(self.number_served) + ' customers.\n')
		
	def open_restaurant(self):
		print(self.restaurant_name + ' is now serving.\n')
	
	def set_number_served(self,num):
		self.number_served = num
		
	def increment_number_served(self):
		self.number_served += 1
		
res = restaurant('blue moon','chinese')
res.describe_restaurant()
res.open_restaurant()

res.number_served = 5
res.describe_restaurant()

res.set_number_served(12)
res.describe_restaurant()

res.increment_number_served()
res.describe_restaurant()

python练习——类_第1张图片

3.类的继承

#9-6
class restaurant():
	def __init__(self,name,cuisine_type,num = 0 ):
		self.restaurant_name = name
		self.cuisine_type = cuisine_type
		self.number_served = num
		
	def describe_restaurant(self):
		print(self.restaurant_name + ' is a(an) ' + self.cuisine_type + ' restaurant.\n')
		print(self.restaurant_name + ' has served ' + str(self.number_served) + ' customers.\n')
		
	def open_restaurant(self):
		print(self.restaurant_name + ' is now serving.\n')
	
	def set_number_served(self,num):
		self.number_served = num
		
	def increment_number_served(self):
		self.number_served += 1
		
		
class IceCreamStand(restaurant):
	def __init__(self,name,cuisine_type,num,*taste):
		super().__init__(name,cuisine_type,num)
		self.flavor = []
		for each in taste:
			self.flavor.append(each)
		
	def describe_tastes(self):
		print('We have ')
		for each in self.flavor:
			print(each + ', ')
		print('which one do you like?\n')
		
flesh_summer = IceCreamStand('flesh_aummer','Chinese',25,'orange','watermelon','strawberry')
flesh_summer.describe_restaurant()
flesh_summer.describe_tastes()

python练习——类_第2张图片

4.类的导入

#9-10
from chapter9_6 import restaurant as res

my_moon = res('moon','Chinese',35)
my_moon.describe_restaurant()

python练习——类_第3张图片

你可能感兴趣的:(python)