有3个小老板开店,分别是辣辣火锅店,中国口味面包店,和7天酒店。现在你要帮他们解决门口招牌的文字供顾客在网上查看。
你在仔细了解需求以后,发现有共同的地方,但酒店不同餐馆,有一个是否有房的判断。如下所示:
描述饮食店的类,包含3个属性:店名、定位和到店客人
class shop():
def __init__(self,name,cuisine):
self.cuisine=cuisine
self.name=name
def describe_shop(self):# 店的招牌
pop = 'welcome to ' + self.name + '' + self.cuisine
return pop
def open_shop(self):# 店“正在营业中”
notice = self.name + ' ' + 'isopening'
return notice
my_shop = shop('lala','hotpot')
your_shop =shop('breadtalk','chinesetyle')
输入:
print (my_shop.name)
print (my_shop.describe_shop())
print (my_shop.open_shop())
print (your_shop.describe_shop())
print (your_shop.open_shop())
输出:
lala
welcome to lala hotpot
lala is opening
welcome to breadtalk chinesetyle
breadtalk is opening
Process finished with exit code 0
7天酒店有一个新的属性需求,需要建新的类,但有店名、客人定位和到店客人都可以继承上面的父类shop。有一个属性是酒店特有的:需要说明是否客满?
假设 number_capacity = 100说明酒店共有100个房间,如果到店的客人预订的房间大于100,那么显示房间全部订出,没有空房,否则可以预定。
class hotel(shop):
def__init__(self,name,cuisine,number_served):
super().__init__(name,cuisine,number_served)
self.number_capacity = 100
def hotel_occupy_status(self):
if self.number_served >self.number_capacity:
occupy_status = 'sorry sir!' +self.name + 's all room be reserved'
else:
occupy_status = 'thank you sir!' +self.name + 'your room is to be reserved'
return occupy_status
输入:
guest_booking = hotel(' 7 days inn','biz',66)
print(guest_booking.hotel_occupy_status())
输出:
thank you sir! 7 days innyour room is to be reserved
输入:
guest_booking = hotel(' 7 days inn','biz',122)
print(guest_booking.hotel_occupy_status())
输出:
sorry sir! 7 days inns all room be reserved
领取专属 10元无门槛券
私享最新 技术干货