9_1餐馆(练习题)
class Restaurant():
"""一次模拟餐厅的简单尝试"""
def __init__(self,name,type):
"""餐厅属性初始化"""
self.restuarant_name = name
self.cuisine_type = type
def describe_restaurant(self):
"""简单介绍餐厅"""
print("The restaurant's name is " + self.restuarant_name + "\n")
print("The cuisine's type is " + self.cuisine_type + "\n")
def open_restaurant(self):
"""餐厅正在营业"""
print("The restaurant is open.")
#初始化餐厅
restaurant_one = Restaurant("mengduo","western food")
#打印餐厅的名字和烹饪类型
print("restuarant name: " + restaurant_one.restuarant_name + "\n")
print("cuisine type:" + restaurant_one.cuisine_type + "\n")
#对餐厅进行介绍
restaurant_one.describe_restaurant()
#餐厅正在营业
restaurant_one.open_restaurant()
----------------------------------------------------------------------------------------------------------------
结果:
在编写该代码的过程中,遇到了两个问题:
1.TypeError:object() takes no parameters
__init__(self,name,type)这个函数左右各是两个下划线!!!而非左右一个!!!
2.TypeError:describe_restaurant() takes 0 positional arguments but 1 was given
作为类中的一个函数,在形参中一定要包含self,这是因为每个与类相关的方法都自动传递self
网友评论