1. ホーム
  2. python

[解決済み] Python : 従業員と生産労働者クラスのプログラム、私は私のエラーがどこにあるのか分からないのですか?

2022-02-09 16:08:20

質問内容

こんにちは、私はエラーがあったのかわからない私は何度も投げて見て、問題を見つけるためにカント継ぎ目と何時間もいじくり回した後、私はそれ以上私が持っているより台無しにしたくない。

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'

をご覧ください。

解決方法は?

以下は実行可能なコードです。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()