Python编程:从入门到实践第九章练习9.3

练习9.3

#coding:gbk

#9-6冰淇淋小店

class Restaurant():
	def __init__(self,restaurant_name,cuisine_type):
		"""初始化属性"""
		self.reataurant_name = restaurant_name
		self.cuisine_type = cuisine_type
		self.number_served = 0
		
	def describe_restaurant(self):
		"""打印名称和类型"""
		print("The restaurant's name is " + self.restaurant_name.title() + " which are specialize in " + self.cuisine_type + ".")
		
	def open_restaurant(self):
		"""打印开业消息"""
		print("The restaurant is opening!")
		
	def having_served(self):
		"""打印一条已就餐人数的消息"""
		print(str(self.number_served) + " people have been served.")
		
	def set_number_served(self,i):
		self.number_served = i
		
	def increment_number_served(self,s):
		self.number_served += s
		
		
class IceCreamStand(Restaurant):
	def __init__(self,restaurant_name,cuisine_type):
		"""初始化父类的属性"""
		super().__init__(restaurant_name,cuisine_type)
		self.flavors = ['apple','strawberry','blueberry']
		
	def describe_icecream(self):
		for flavor in self.flavors:
			print(flavor.title() + "Ice Cream")
icecream = IceCreamStand('john','american')
icecream.describe_icecream()

#9-7管理员
class User():
	def __init__(self,first_name,last_name,age,location):
		"""设置初始属性"""
		self.first_name = first_name
		self.last_name = last_name
		self.age = age
		self.location = location
		
	def describe_user(self):
		"""打印用户信息摘要"""
		print("\nFirst Name: " + self.first_name.title())
		print("Last Name: " + self.last_name.title())
		print("Age: " + str(self.age))
		print("Location: " + self.location)
		
	def greeter_user(self):
		"""向用户发出个性问候"""
		print("Nice to meet you, " + self.first_name.title() + " " + self.last_name.title() + "!")
		
class Admin(User):
	
	def __init__(self,first_name,last_name,age,location):
		"""初始化父类属性"""
		super().__init__(first_name,last_name,age,location)
		self.privileges = ['can add post','can delete post','can ban user']
		
	def show_privileges(self):
		for privileges in self.privileges:
			print(self.first_name.title() + " " + self.last_name.title() + " " + privileges + ".")
			
administer = Admin('john','su',20,'Amoy')
administer.show_privileges()


#9-8权限

class Privileges():
	def __init__(self,privileges = ['can add post','can delete post','can ban user']):
		"""初始化属性"""
		self.privileges = privileges
		
	def show_privileges(self):
		for privileges in self.privileges:
			print(privileges)
			
	
class Admin(User):
	
	def __init__(self,first_name,last_name,age,location):
		"""初始化父类属性"""
		super().__init__(first_name,last_name,age,location)
		self.privileges = Privileges()
		
	
			
administer = Admin('john','su',20,'Amoy')
administer.privileges.show_privileges()

#并未完成,如何在分成小类时将名字等属性传递给小类?

#9-9电瓶升级

class Car():                             
	"""一次模拟汽车的尝试"""
	
	def __init__(self,make,model,year):
		self.make = make
		self.model = model
		self.year = year
		self.odometer_reading= 0
		
	def get_descriptive_name(self):
		long_name = str(self.year) + ' ' + self.make + ' ' + self.model
		return long_name.title()
		
	def read_odometer(self):
		print("This car has " + str(self.odometer_reading) + " miles on it.")
	
	def update_odometer(self,mileage):
		if mileage >= self.odometer_reading:
			self.odometer_reding = mileage
		else:
			print("You can't roll back an odometer!")
	def increment_odometer(self,miles):
		self.odometer += miles
		
class Battery():
	"""一次 模拟电动汽车电瓶的简单尝试"""
	
	def __init__(self,battery_size=70):
		"""初始化电瓶属性"""
		self.battery_size = battery_size
		
	def describe_battery(self):
		"""打印一条描述电瓶容量的消息"""
		print("This car has a " + str(self.battery_size) + "-kwh battery.")
		
	def get_range(self):
		"""打印一条消息,指出电瓶的续航里程"""
		if self.battery_size == 70:
			range =240
		elif self.battery_size == 85:
			range = 270
		message = "This car can go approximately " + str(range)
		message += " miles on a full range."
		print(message)
			
	def upgrade_battery(self):
		if self.battery_size < 85:
			self.battery_size = 85
		

class ElectricCar(Car):
	"""电动车的独特之处"""
	
	def __init__(self,make,model,year):
		"""
		初始化父类的属性,再初始化电动汽车所有的属性
		"""
		super().__init__(make,model,year)
		self.battery = Battery()

my_tesla = ElectricCar('tesla','model s',2016)
print(my_tesla.get_descriptive_name())
my_tesla.battery.describe_battery()

my_tesla.battery.get_range()

my_tesla.battery.upgrade_battery()
my_tesla.battery.get_range()



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值