在继承和子类

时间:2017-04-18 23:21:01

标签: python inheritance

我试图用子类编写程序。我已经调试好几个小时了,无法找到解决方案。

我需要编写一个Employee类来保存Employee name和Employee number的数据属性。接下来,我需要编写一个名为ProductionWorker的类,它是Employee类的子类。 ProductionWorker类应该保留本周工作班次,小时工资率和工时数的数据属性。工作日分为两个班次:白天和黑夜。 shift属性将包含一个表示员工工作班次的整数值。白班为1,夜班为2.为每个班级编写适当的访问者和改变者方法。

接下来,我需要创建ProductionWorker类的对象,并提示用户输入每个对象的数据属性的数据。我使用数据验证类来确保用户输入付费率和小时数的有效值。我将数据存储在对象中,然后使用对象的访问器方法来检索它并在屏幕上显示它。我还需要显示这个工人本周的总工资。总工资=每小时工资*工作小时数。我无法对主文件进行任何计算。

接下来,我需要编写一个ShiftSupervisor类,它是Employee类的一个独立子类。如果班次符合生产目标,轮班主管可以获得薪水和年度奖金。 ShiftSupervisor类应保留年薪的数据属性和轮班主管获得的年度生产奖金的数据属性。
我需要编写第二个程序来创建ShiftSupervisor类的对象,并提示用户输入每个对象的数据属性的数据。我需要显示这位主管的年薪总额。

当我运行主文件时,我收到此错误:

        Traceback (most recent call last):
  File "/Users/Jeremy/Documents/Python Projects/Mosier_Jeremy_HW8/HW8MAIN.py", line 4, in <module>
    employeeObject = EmployeeFile.ProductionWorker ()
TypeError: __init__() missing 5 required positional arguments: 'name', 'number', 'shift', 'rate', and 'hours'

这是我的ValidationFile:

    class ValidationClass:

    def checkFloat (self, inputString):

        try:
            result = float (inputString)
        except Exception:
            return -1

        if result < 0:
            return -1
        else:
            return result

    def checkInteger (self, inputString):

        try:
            result = int (inputString)
        except Exception:
            return -1

        if result < 0:
            return -1
        else:
            return result

这是我的EmployeeFile:

    class Employee ():
    def __init__(self, name, number):
        self.__Name = name
        self.__Number = number

    def set_Name (self, name):
        self.__Name = value

    def set_Number(self, number):
        self.__Number = value

    def get_Name (self):
        return self.__Name

    def get_Number (self):
        return self.__Number

class ProductionWorker (Employee):
    def __init__(self, name, number, shift, rate, hours):
        self.__Shift = shift
        self.__Rate = rate
        self.__Hours = hours
        Employee.__init__(self, name, number)

    def set_Shift (self, shift):
        self.__Shift = shift

    def set_PayRate (self, rate):
        self.__PayRate = rate

    def set_Hours (self, hours):
        self.__Hours = hours

    def get_Shift (self):
       if self.__Shift == 1:
            s = 'Day shift'
       elif self.__Shift == 2:
            s = 'Night shift'
       return s

    def get_PayRate (self):
        return self.__PayRate

    def get_Hours (self):
        return self.__Hours

    def get_Pay (self):
        return self.__Pay

    def calcPay (self):
        self.__Pay = (self.__PayRate) * (self.__Hours)

class ShiftSupervisor (Employee):
    def __init__ (self, name, number, salary, bonus):
        self.__Salary = salary
        self.__Bonus = bonus
        Employee.__init__ (self, name, number)

    def set_Salary (self, salary):
        return self.__Salary

    def set_Bonus (self, bonus):
        return self.__Bonus

    def get_Salary (self):
        self.__Salary = salary

    def get_Bonus (self):
        self.__Bonus = bonus

    def calcPay (self):
        self.__Pay = (self.__Salary) + (self.__Bonus)

这是我的主要文件:

    import EmployeeFile
import ValidationFile

employeeObject = EmployeeFile.ProductionWorker ()
validationObject = ValidationFile.ValidationClass ()

employeeName = -1
while employeeName == -1:
    employeeName = input ('Please enter the employee name: ')
    if employeeName == '':
        print ('ERROR: Please enter a valid name.')
        employeeName = -1

employeeNumber = -1
while employeeNumber == -1:
    employeeNumber = input ('Please enter the employee name: ')
    if employeeNumber == '':
        print ('ERROR: Please enter a valid name.')
        employeeNumber = -1

shiftNumber = -1
while shiftNumber == -1:
    shiftNumber = input ('Please enter which shift the employee works. 1 for day shift, 2 for night shift: ')
    if shiftNumber < 1 or shiftNumber > 2:
        print ('ERROR: Shift number must be entered as a 1 or a 2.')

payRate = -1
while payRate == -1:
    payEntry = input ('Please enter which shift the employee works. 1 for day shift, 2 for night shift: ')
    payRate = validationObject.checkFloat (payEntry)
    if payRate == -1:
        print ('ERROR: Pleae enter a valid payRate.')

hours = -1
while hours == -1:
    hoursEntry = input ('Please enter which shift the employee works. 1 for day shift, 2 for night shift: ')
    payRate = validationObject.checkFloat (hoursEntry)
    if payRate == -1:
        print ('ERROR: Pleae enter a valid payRate.')

#populate inputs
employeeObject.set_Name (employeeName)
employeeObject.set_Number (employeeNumber)
employeeObject.set_Shift (shiftNumber)
employeeObject.set_PayRate (payRate)
employeeObject.set_Hours (hours)

print ('Employee name: ', employeeName)
print ('Employee number: ', employeeNumber)
print ('Shift: ', shiftNumber)
print ('Pay rate: ', payRate)
print ('Hours worked: ', hours)

2 个答案:

答案 0 :(得分:0)

您的ProductionWorker类要求您输入某些参数,这就是它抱怨的原因:

class ProductionWorker (Employee):
    def __init__(self, name, number, shift, rate, hours):
        self.__Shift = shift
        self.__Rate = rate
        self.__Hours = hours
        Employee.__init__(self, name, number)

在获得所有信息之前,您需要使这些可选或不创建对象。我建议您等到获得所有信息,然后用以下内容替换当前的填充输入:

#populate inputs
employeeObject = EmployeeFile.ProductionWorker(employeeName, employeeNumber, shiftNumber, payRate, hours)

此外,您应该将super()视为调用父类init的正确方法。

答案 1 :(得分:0)

您必须在程序中调用构造函数,因为您已在 init 方法中声明了几个变量:

在主程序中:

employeeObject = EmployeeFile.ProductionWorker("name", 12, 1, 10.0, 3)#random variable names used