这是一个程序,它应该提出一系列问题,保存信息,然后将信息显示给您。每当我运行该程序时,它都会成功询问所有信息,但在尝试显示其中的某个部分时会出错。
错误是:
Traceback (most recent call last):
, line 27, in <module>
main()
, line 24, in main
print ("Shift: ", worker.get_shift_number())
AttributeError: 'Worker' object has no attribute 'get_shift_number'
代码是:
# employee.py
class Employee(object):
def __init__(self, name, id_number):
self.__id_number = id_number
self.__name = name
def set_name(self, name):
self.__name = name
def set_id_number(self, id_number):
self.__id_number = id_number
def get_name(self):
return self.__name
def get_id_number(self):
return self.__id_number
class Worker(Employee):
def __init__(self, name, id_number, shift_number, pay_rate):
#call superclass __init__ method
Employee.__init__(self, name, id_number)
#initialize shift_number and pay_rate attributes
self.__shift_number = shift_number
self.__pay_rate = pay_rate
#mutator functions for shift_number and pay_rate
def set_shift_number(self, shift_number):
self.__shift_number = shift_number
def set_pay_rate(self, pay_rate):
self.__pay_rate = pay_rate
#accessor functions for shift_number and pay_rate
def get_shift_number(self):
return self.__shift_number
def get_pay_rate(self):
return self.__pay_rate
然后我们有这个。
#workerprogram.py
import employee
def main():
#variables
worker_name= " "
worker_id = " "
worker_shift = 0
worker_pay = 0.0
#get data attributes
worker_name = input("Enter the worker name: ")
worker_id = input("Enter the ID number: ")
worker_shift = float(input("Enter the shift number: "))
worker_pay = float(input("Enter the hourly pay rate: "))
#create an instance of Worker
worker = employee.Worker(worker_name, worker_id, worker_shift, worker_pay)
#display information
print ("Production worker information: ")
print ("Name: ", worker.get_name())
print ("ID number: ", worker.get_id_number())
print ("Shift: ", worker.get_shift_number())
print ("Hourly Pay Rate: $", worker.get_pay_rate())
main()
如前所述,缩进是错误的,而且我还看到我的 shift_number 初始化在错误的位置。;(
谢谢!