第五周 ——第九章《类》作业

9-1 餐馆 : 创建一个名为 Restaurant 的类, 其方法 __init__() 设置两个属性: restaurant_name cuisine_type 。 创建一个名
describe_restaurant() 的方法和一个名为 open_restaurant() 的方法, 其中前者打印前述两项信息, 而后者打印一条消息, 指出餐馆正在营业。

根据这个类创建一个名为restaurant 的实例, 分别打印其两个属性, 再调用前述两个方法。

class Restaurant():
    def __init__(self,name, type):
        self.restaurant_name= name
        self.cuisine_type= type
    def describle_restaurant(self):
        print('this restaurant called ',self.restaurant_name,' sell ',self.cuisine_type)
    def open_restaurant(self):
        print('this rastaurant is opening! ')

my_restaurant=Restaurant('happymore','chinese_food')
print(my_restaurant.restaurant_name)
print(my_restaurant.cuisine_type)
my_restaurant.describle_restaurant()
my_restaurant.open_restaurant()

输出

happymore
chinese_food
this restaurant called  happymore  sell  chinese_food
this rastaurant is opening! 

9-2 三家餐馆 : 根据你为完成练习9-1而编写的类创建三个实例, 并对每个实例调用方法describe_restaurant()

class Restaurant():
    def __init__(self,name, type):
        self.restaurant_name= name
        self.cuisine_type= type
    def describle_restaurant(self):
        print('this restaurant called ',self.restaurant_name,' sell ',self.cuisine_type)
    def open_restaurant(self):
        print('this rastaurant is opening! ')

my_restaurant=Restaurant('happymore','chinese_food')
my_restaurant.describle_restaurant()
your_restaurant=Restaurant('Nicy','Italy_food')
your_restaurant.describle_restaurant()
her_restaurant=Restaurant('Fly','fast_food')
her_restaurant.describle_restaurant()

输出

this restaurant called  happymore  sell  chinese_food
this restaurant called  Nicy  sell  Italy_food
this restaurant called  Fly  sell  fast_food
9-4 就餐人数 : 在为完成练习 9-1 而编写的程序中, 添加一个名为 number_served 的属性, 并将其默认值设置为 0 。 根据这个类创建一个名为 restaurant 的实
例; 打印有多少人在这家餐馆就餐过, 然后修改这个值并再次打印它。
添加一个名为
set_number_served() 的方法, 它让你能够设置就餐人数。 调用这个方法并向它传递一个值, 然后再次打印这个值。
添加一个名为
increment_number_served() 的方法, 它让你能够将就餐人数递增。 调用这个方法并向它传递一个这样的值: 你认为这家餐馆每天可能接待的就

餐人数。

class Restaurant():
    def __init__(self,name, type,num=0):
        self.restaurant_name= name
        self.cuisine_type= type
        self.guest_number=num

    def print_num(self):
        print('there are ',self.guest_number,' people eating dinner. ')

    def set_number_served(self,num):
        self.guest_number=num
    
    def increment_number_served(num):
        self.guest_number += num

my_restaurant=Restaurant('happymore','chinese_food',5)
my_restaurant.print_num()
my_restaurant.set_number_served(10)
my_restaurant.print_num()

输出

there are  5  people eating dinner. 
there are  10  people eating dinner. 
9-6 冰淇淋小店 : 冰淇淋小店是一种特殊的餐馆。 编写一个名为 IceCreamStand 的类, 让它继承你为完成练习 9-1 或练习 9-4 而编写的 Restaurant 类。 这两个版
本的
Restaurant 类都可以, 挑选你更喜欢的那个即可。 添加一个名为 flavors 的属性, 用于存储一个由各种口味的冰淇淋组成的列表。 编写一个显示这些冰淇淋

的方法。 创建一个IceCreamStand 实例, 并调用这个方法。

class Restaurant():
    def __init__(self,name, type):
        self.restaurant_name= name
        self.cuisine_type= type
       
class IcecreamStand(Restaurant):
    def __init__(self,name, type,flavors):
        super().__init__(name, type)
        self.flavor = []
        for x in flavors:
            self.flavor.append(x)
        

    def print_flavor(self):
        print('this IcecreamStand have ',end="")
        for x in self.flavor:
            print(x,' ',end="")
        print('.')

flavors=['chocalate','chese','apple']
my_icecream=IcecreamStand('Best','ice cream',flavors)
my_icecream.print_flavor()

输出

this IcecreamStand have chocalate  chese  apple  .

9-10 导入Restaurant类 :将最新的Restaurant 类存储在一个模块中。在另一个文件中,导入Restaurant 类,创建一个Restaurant 实例,并调用Restaurant 的一个方法,以确认import 语句正确无误。 

(restaurant.py)

class Restaurant():
    def __init__(self,name, type):
        self.restaurant_name= name
        self.cuisine_type= type
       
class IcecreamStand(Restaurant):
    def __init__(self,name, type,flavors):
        super().__init__(name, type)
        self.flavor = []
        for x in flavors:
            self.flavor.append(x)
        

    def print_flavor(self):
        print('this IcecreamStand have ',end="")
        for x in self.flavor:
            print(x,' ',end="")
        print('.')

(IcecreanStand.py)

from restaurant import IcecreamStand

flavors=['chocalate','chese','apple']
my_icecream=IcecreamStand('Best','ice cream',flavors)
my_icecream.print_flavor()
    

输出

this IcecreamStand have chocalate  chese  apple  .




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值