简体   繁体   中英

Class inheritance? Python?

Here is a program that is supposed to ask a series of questions, save the information, and then display the information back to you. Whenever I run the program, it successfully asks for all of the information, but gives an error when it tries to display a certain part of it.

The error is:

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'

The code is:

# 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

Then we have this.

#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()

Indention was wrong like noted, and I also saw that I have my shift_number initializing in the wrong spot. ;(

Thank you!

Since Rob deleted his correct answer: Your problem is that your indentation is wrong.

However, that's not the only problem with the code. The main one is that its' over complicated as you have accessors/mutators for everything, which is not needed, and will just wast time both for you typing them in and the computer using them.

This is how your code should look:

# employee.py
class Employee(object):

    def __init__(self, name, id_number):
        self.id_number = id_number
        self.name = name

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

#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.name)
    print ("ID number: ", worker.id_number)
    print ("Shift: ", worker.shift_number)
    print ("Hourly Pay Rate: $", worker.pay_rate)

main()

Although admittedly I don't think you actually want neither shift number nor pay rate as floats, but that can wait to another question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM