Python:Employee和Production Worker Classes程序,我不知道我的错误在哪里?

时间:2015-04-21 03:51:46

标签: python

嗨我不知道是错误是我看了几次,并且无法找到问题而且在修补之后我不想再把它搞砸了然后我已经准备好了。

class Employee:

    def __init__(self,name,number):
        self.__name = name
        self.number = number
    def set_emp_name(self,name):
        self.__name = name
    def set_emp_number(self,number):
        self.__number = number
    def get_emp_name(self):
        return self.__number
    def get_emp_number(self):
        return self.__number

class ProductionWorker(Employee):

    def __init__(self,name,number,shift_num,pay_rate):
        Employee.__init__(self,name,number)
        self.__shift_num = shift_num
        self.__pay_rate = pay_rate
    def set_shift_num(self,shift_num):
        self.__shift_num = shift_num
    def set_pay_rate(self,pay_rate):
        self.__pay_rate = pay_rate
    def get_shift_num(self):
        return self.__shift_num
    def get_pay_rates(self):
        return self.__pay_rate

def main():

    print('enter following Details of the Employee')
    name = input ('Enter Employee Name: ')
    number = input ('Enter Employee Number: ')
    sh= input ('Enter Shift Number: ')
    pay = input ('Enter Pay Rate: ')

    emp = ProductionWorker(name,number,sh,pay)

    print ('Details of Employee:')
    print ('Name',emp.get_emp_name())
    print ('Employee Number',emp.get_emp_number())
    print ('Shift Number',emp.get_shift_number())
    print ('Pay Rate',emp.get_pay_rate())


main() 

输出是:

enter following Details of the Employee
Enter Employee Name: john
Enter Employee Number: 12
Enter Shift Number: 2
Enter Pay Rate: 10

Details of Employee:
Traceback (most recent call last):
  File "D:/chapter11probelm1.py", line 48, in <module>
    main()
  File "D:/chapter11probelm1.py", line 42, in main
    print ('Name',emp.get_emp_name())
  File "D:/chapter11probelm1.py", line 12, in get_emp_name
    return self.__number
AttributeError: 'ProductionWorker' object has no attribute '_Employee__number'

任何帮助将不胜感激。

4 个答案:

答案 0 :(得分:1)

Python破坏属性名称,这些名称仅以双下划线开头,并带有当前类的名称。这打破了这些属性的继承。将您想要私有的属性更改为使用单个下划线。

答案 1 :(得分:1)

正如回溯所示,错误发生在第12行(回溯的最后一位是堆栈的最高位)。

def get_emp_name(self):
    return self.__number  # <- This is line 12

在您的班级__init__Employee)中,您定义的是self.number,而在第12行,您正在访问self.__number。你看到那里的问题吗?

答案 2 :(得分:1)

这是您可以尝试的可运行代码。我使用PyCharm让我轻松找出错误的位置:

class Employee:

    def __init__(self,name,number):
        self.__name = name
        self.number = number
    def set_emp_name(self,name):
        self.__name = name
    def set_emp_number(self,number):
        self.__number = number
    def get_emp_name(self):
        return self.__name
    def get_emp_number(self):
        return self.number

class ProductionWorker(Employee):

    def __init__(self,name,number,shift_num,pay_rate):
        Employee.__init__(self,name,number)
        self.__shift_num = shift_num
        self.__pay_rate = pay_rate
    def set_shift_num(self,shift_num):
        self.__shift_num = shift_num
    def set_pay_rate(self,pay_rate):
        self.__pay_rate = pay_rate
    def get_shift_num(self):
        return self.__shift_num
    def get_pay_rates(self):
        return self.__pay_rate

def main():

    print('enter following Details of the Employee')
    emp_name = input('Enter Employee Name: ')
    number = input('Enter Employee Number: ')
    sh = input('Enter Shift Number: ')
    pay = input('Enter Pay Rate: ')

    emp = ProductionWorker(emp_name, number, sh, pay)

    print ('Details of Employee:')
    print ('Name',emp.get_emp_name())
    print ('Employee Number',emp.get_emp_number())
    print ('Shift Number',emp.get_shift_num())
    print ('Pay Rate',emp.get_pay_rates())

if __name__ == '__main__':
    main()

答案 3 :(得分:0)

感谢您提供代码正在运行的帮助

班级员工:

def __init__(self,name,number):
    self.__name = name
    self.number = number
def set_emp_name(self,name):
    self.__name = name
def set_emp_number(self,number):
    self.__number = number
def get_emp_name(self):
    return self.__name
def get_emp_number(self):
    return self.number

class ProductionWorker(Employee):

def __init__(self,name,number,shift_num,pay_rate):
    Employee.__init__(self,name,number)
    self.__shift_num = shift_num
    self.__pay_rate = pay_rate
def set_shift_num(self,shift_num):
    self.__shift_num = shift_num
def set_pay_rate(self,pay_rate):
    self.__pay_rate = pay_rate
def get_shift_num(self):
    return self.__shift_num
def get_pay_rates(self):
    return self.__pay_rate

def main():

print('enter following Details of the Employee')
name = input ('Enter Employee Name: ')
number = input ('Enter Employee Number: ')
sh= input ('Enter Shift Number: ')
pay = input ('Enter Pay Rate: ')

emp = ProductionWorker(name,number,sh,pay)

print ('Deails of Employee:')
print ('Name: ',emp.get_emp_name())
print ('Employee Number: ',emp.get_emp_number())
print ('Shift Number: ',emp.get_shift_num())
print ('Pay Rate: ',emp.get_pay_rates())

main()