先来看下源代码:
#coding=gbk
class Restaurant():
def _init_(self,restaurant_name,cuisine_type):
self.restaurant_name = restaurant_name
self.cuisine_type = cuisine_type
def describe_restaurant(self):
print('name='+self.restaurant_name+' cuisine_type='+self.cuisine_type)
def describe_restaurantstatus(self):
print('restaurant is opening')
my_restaurant = Restaurant('川菜馆','北京菜')
print(my_restaurant.describe_restaurant())
乍一看没有什么问题,类是Restaurant,大小写也都有,初始化方法也有, 最后调用的Restaurant创建实例也有参数,
就是提示“object() takes no parameters”,意思是实例方法没有参数,默认是调用无参数的方法,但是我们有参数啊?
咋解决?其实代码没有问题,唯一一个问题是,"_init_" 的前面和后面 应该各是两个横线"__",不是一个"_",是这样:
"__init__",这个错误不太好找,建议大家记住了,以后写的时候千万要注意!不然这个错误很难找到!