python 类


#9-1  餐馆 :创建一个名为 Restaurant 的类,其方法 __init__() 设置两个属性: restaurant_name 和 cuisine_type 。创建一个名
#为 describe_restaurant() 的方法和一个名为 open_restaurant() 的方法,其中前者打印前述两项信息,而后者打印一条消息,指出餐馆正在营业。
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(self.restaurant_name)
  print(self.cuisine_type)
 def open_restaurant(self):
  print(self.restaurant_name+"is opening")
restaurant = Restaurant('oooo','dwdwdw')
restaurant.describe_restaurant()
restaurant.open_restaurant()


#9-2  三家餐馆 :根据你为完成练习 9-1 而编写的类创建三个实例,并对每个实例调用方法 describe_restaurant() 。
restaurant1 = Restaurant('cccc','dwdwdw')
restaurant2 = Restaurant('aaaa','dwdwdw')
restaurant3 = Restaurant('bbbb','dwdwdw')
restaurant1.describe_restaurant()
restaurant2.describe_restaurant()
restaurant3.describe_restaurant()


#9-4  就餐人数 :在为完成练习 9-1 而编写的程序中,添加一个名为 number_served 的属性,并将其默认值设置为 0 。根据这个类创建一个名为 restaurant 的实
#例;打印有多少人在这家餐馆就餐过,然后修改这个值并再次打印它。
print(restaurant.restaurant_name+" serve "+ str(restaurant.number_served)+" people")
restaurant.number_served = 1
print(restaurant.restaurant_name+" serve "+ str(restaurant.number_served)+" people")

#9-6  冰淇淋小店 :冰淇淋小店是一种特殊的餐馆。编写一个名为 IceCreamStand 的类,让它继承你为完成练习 9-1 或练习 9-4 而编写的 Restaurant 类。这两个版
#本的 Restaurant 类都可以,挑选你更喜欢的那个即可。添加一个名为 flavors 的属性,用于存储一个由各种口味的冰淇淋组成的列表。编写一个显示这些冰淇淋
#的方法。创建一个 IceCreamStand 实例,并调用这个方法。
class IceCreamStand(Restaurant):
 def __init__(self,restaurant_name,cuisine_type,flavors):
  super().__init__(restaurant_name,cuisine_type)
  self.flavors = flavors
  
 def show_flavors(self):
  for flavor in self.flavors:
   print(flavor)
ice = IceCreamStand("ff","dw",["rose",'apple'])
ice.show_flavors()


#9-7 导入类
from my_class import Restaurant
restaurant = Restaurant("name","rose")
restaurant.describe_restaurant()

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值