练习9-1:餐馆 创建一个名为Restaurant 的类,为其方法__init__()
设置属性restaurant_name 和cuisine_type 。创建一个名为
describe_restaurant() 的方法和一个名为open_restaurant() 的方
法,前者打印前述两项信息,而后者打印一条消息,指出餐馆正在营业。
根据这个类创建一个名为restaurant 的实例,分别打印其两个属性,再调用
前述两个方法。
class Restaurant:
def __init__(self, restaurand_name, cuisine_type):
self.restaurant_name = restaurand_name
self.cuisine_type = cuisine_type
def describe_restaurant(self):
print(f"这个餐馆的名字是{self.restaurant_name},菜系是{self.cuisine_type}")
def open_restaurant(self):
print(f"{self.restaurant_name}餐馆正在营业!")
restaurant = Restaurant('好运来', '东北菜')
restaurant.describe_restaurant()
restaurant.open_restaurant()
这个餐馆的名字是好运来,菜系是东北菜
好运来餐馆正在营业!
进程已结束,退出代码0
这段代码展示了如何在Python中定义一个名为Restaurant的类,包含初始化方法__init__(),用于设置餐馆名称和菜系类型属性。另外,定义了describe_restaurant()方法来展示餐馆信息,以及open_restaurant()方法来显示餐馆营业状态。示例中创建了一个名为'好运来'的东北菜餐馆实例,并调用了相应方法打印信息。
4483

被折叠的 条评论
为什么被折叠?



