class Restaurant():
def __init__(self,restaurant_name,cuisine_type): “”“初学很容易出现 TypeError: __init__() takes exactly
3
arguments (
4
given)的错误"""
"""究其原因,是要明白__init__()函数里只设置了两个形参“”“
”“”而你在接下来调用方法descript()时,直接设置了三个实参“”“
”“”你可以通过设置三个形参或者增加另外的方法,如下面的set_number_served()“”“
”“”通过调用set方法设置属性值后再调用descript()即可“”“
self.restaurant_name = restaurant_name self.cuisine_type = cuisine_type self.number_served = 300 def describe_restaurant(self): print(self.restaurant_name + " " + self.cuisine_type + " " + str(self.number_served) + ": ") def open_restaurant(self): print(" is openning!\n") def set_number_served(self,number): self.number_served = number def adding(self,num):“”“这里要注意先将adding_number置0,如果直接adding_number自加的话会得到你设置值的两倍"""
self.adding_number = 0 if self.adding_number >= 0: self.adding_number = self.adding_number + num print("We can serve: " + str(self.adding_number) + " person!") else: print("You can below adding_number!")restaurant = Restaurant('Jerry&Eurus','sweetmeats')restaurant.describe_restaurant()restaurant.set_number_served(350)restaurant.describe_restaurant()restaurant.adding(50) posted on 2017-11-14 18:57 阅读( ...) 评论( ...) 收藏