我已经声明了我的班级Customer
,并且正在尝试从类实例中提取数据,而我的方法get_place_in_queue
工作正常,但是当我运行get_nr_errands
时,它会出现错误:
"name 'nr_errands' is not defined"
到目前为止我的代码:
class Customer:
def __init__(self,index_number,place_in_queue,nr_errands):
self.index_number=index_number
self.place_in_queue=place_in_queue
self.nr_errands=nr_errands
def get_place_in_queue(self):
return place_in_queue
def get_index_number(self):
return index_number
def get_nr_errands(self):
return nr_errands
test=Customer(2,4,5)
test.get_nr_errands()
答案 0 :(得分:2)
您忘记使用self
来访问实例变量。你需要:
class Customer:
def __init__(self,index_number,place_in_queue,nr_errands):
self.index_number=index_number
self.place_in_queue=place_in_queue
self.nr_errands=nr_errands
def get_place_in_queue(self):
return self.place_in_queue # <-- here
def get_index_number(self):
return self.index_number # <-- here
def get_nr_errands(self):
return self.nr_errands # <-- here