#coding:gbk#9-6冰淇淋小店classRestaurant():def__init__(self,restaurant_name,cuisine_type):"""初始化属性"""
self.reataurant_name = restaurant_name
self.cuisine_type = cuisine_type
self.number_served =0defdescribe_restaurant(self):"""打印名称和类型"""print("The restaurant's name is "+ self.restaurant_name.title()+" which are specialize in "+ self.cuisine_type +".")defopen_restaurant(self):"""打印开业消息"""print("The restaurant is opening!")defhaving_served(self):"""打印一条已就餐人数的消息"""print(str(self.number_served)+" people have been served.")defset_number_served(self,i):
self.number_served = i
defincrement_number_served(self,s):
self.number_served += s
classIceCreamStand(Restaurant):def__init__(self,restaurant_name,cuisine_type):"""初始化父类的属性"""super().__init__(restaurant_name,cuisine_type)
self.flavors =['apple','strawberry','blueberry']defdescribe_icecream(self):for flavor in self.flavors:print(flavor.title()+"Ice Cream")
icecream = IceCreamStand('john','american')
icecream.describe_icecream()#9-7管理员classUser():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
defdescribe_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)defgreeter_user(self):"""向用户发出个性问候"""print("Nice to meet you, "+ self.first_name.title()+" "+ self.last_name.title()+"!")classAdmin(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']defshow_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权限classPrivileges():def__init__(self,privileges =['can add post','can delete post','can ban user']):"""初始化属性"""
self.privileges = privileges
defshow_privileges(self):for privileges in self.privileges:print(privileges)classAdmin(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电瓶升级classCar():"""一次模拟汽车的尝试"""def__init__(self,make,model,year):
self.make = make
self.model = model
self.year = year
self.odometer_reading=0defget_descriptive_name(self):
long_name =str(self.year)+' '+ self.make +' '+ self.model
return long_name.title()defread_odometer(self):print("This car has "+str(self.odometer_reading)+" miles on it.")defupdate_odometer(self,mileage):if mileage >= self.odometer_reading:
self.odometer_reding = mileage
else:print("You can't roll back an odometer!")defincrement_odometer(self,miles):
self.odometer += miles
classBattery():"""一次 模拟电动汽车电瓶的简单尝试"""def__init__(self,battery_size=70):"""初始化电瓶属性"""
self.battery_size = battery_size
defdescribe_battery(self):"""打印一条描述电瓶容量的消息"""print("This car has a "+str(self.battery_size)+"-kwh battery.")defget_range(self):"""打印一条消息,指出电瓶的续航里程"""if self.battery_size ==70:range=240elif self.battery_size ==85:range=270
message ="This car can go approximately "+str(range)
message +=" miles on a full range."print(message)defupgrade_battery(self):if self.battery_size <85:
self.battery_size =85classElectricCar(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()