做一个叫餐厅的课程。Restaurant的init()方法应该存储两个属性:餐厅名称和菜肴类型。创建一个名为descripe_restaurant()的方法打印这两条信息,创建一个名为open_restaurant()的方法,该方法打印一条指示餐厅已营业的消息。
在你的课上创建一个名为restaurant的实例。分别打印两个属性,然后调用两个方法。在class Restaurant():
"""An attempt to model a restaurant"""
def __init__(self,restaurant_name,cuisine_type):
"""Initializing name and age attributes"""
self.name = restaurant_name
self.cuisine = cuisine_type
def describe_restaurant(self):
# Describes the restaurant name and cuisine
print(self.name.title() + " serves " + self.cuisine + " food.")
def open_restaurant(self):
# Opens the restaurant and welcomes customers.
print(self.name.title() + " is now Open. \nCome. \nHave some delicious " + self.cuisine + " food.")
restaurant1 = Restaurant("big chill","italian")
print(restaurant1.name)
print(restaurant1.cuisine)
restaurant1.describe_restaurant()
我得到的输出是:
^{pr2}$
为什么Python要把类的方法解释为属性?