我可以添加到继承函数吗?

时间:2014-11-25 01:13:07

标签: python class inheritance override

我有以下代码:

    import locale
locale.setlocale(locale.LC_ALL, '') #Sets the locale to 'English_Canada.1252'
class Employee():
    #Initlizes all of the info I need from the user
    def __init__(self, lastName, firstName, payRate):
        self.nameL = lastName
        self.nameF = firstName
        self.payRate = payRate #This payRate is an hourly pay rate
    #Prints out the first and last name of the Employee in the form Employee(FirstName LastName)
    def __repr__(self):
        return('Employee(' + self.nameF + ' ' + self.nameL + ')')
    #Changes the '+' key to add the hourlyPayRate of 2 Employee class types together   
    def __add__(self, otherSelf):
        sumOfPay = self.payRate + otherSelf.payRate
        return(sumOfPay)
    def printCheque(self, numberOfHoursWorked):
        if (numberOfHoursWorked > 40):
           grossIncome = (numberOfHoursWorked - 40) * (self.payRate * 2)
           grossIncome = grossIncome + (40 * self.payRate)           
        else:
            grossIncome = numberOfHoursWorked * self.payRate
        if(grossIncome > 42000):
            taxPaid = grossIncome * 0.22
        else:
            taxPaid = grossIncome * 0.15
        moneyMade = grossIncome - taxPaid           
        print('-'*80 + '\n')
        print('PAY TO: '+ self.nameF + ' ' + self.nameL + ' '*38 + 'AMOUNT: ' + locale.currency(moneyMade)+'\n')
        print('\n')
        print('Gross Pay: '+locale.currency(grossIncome) + '\n')
        print('Deductions: \n')
        print('   Tax     ',locale.currency(taxPaid), '\n')       
        return('-'*78)  
class SalariedEmployee(Employee):
    ''
    #payRate inherited from Employee will refer to salary here.

我必须创建另一个类似于员工的类,但是这个人在一小时内获得工资。我需要做的第一件事是将payRate改为薪水。我不确定这会是什么样的,我尝试了一些东西,但它们没有用。

我需要做的另一件事是更改printCheque以包含其他税,并显示休假时间。我是否可以在不定义新功能的情况下将这样的内容添加到函数中,或者我是否必须创建一个全新的函数?

不确定如何做到这些,你可以给予任何帮助。

谢谢!

2 个答案:

答案 0 :(得分:0)

我可以建议再重构一次...... 似乎每小时和受薪员工是两种不同类型的员工,应该扩展基础员工类。

class Employee():
    #Initlizes all of the info I need from the user
    def __init__(self, lastName, firstName):
        self.nameL = lastName
        self.nameF = firstName

    def __repr__(self):
        return('Employee(' + self.nameF + ' ' + self.nameL + ')')

    def printCheque(self, grossIncome):
        if(grossIncome > 42000):
            taxPaid = grossIncome * 0.22
        else:
            taxPaid = grossIncome * 0.15
        moneyMade = grossIncome - taxPaid           
        print('-'*80 + '\n')
        print('PAY TO: '+ self.nameF + ' ' + self.nameL + ' '*38 + 'AMOUNT: ' + locale.currency(moneyMade)+'\n')
        print('\n')
        print('Gross Pay: '+locale.currency(grossIncome) + '\n')
        print('Deductions: \n')
        print('   Tax     ',locale.currency(taxPaid), '\n')       
        return('-'*78)


class HourlyEmployee(Employee):
    def __init__(self, lastName, firstName, payRate):
        super(HourlyEmployee, self).__init__(lastName, firstName)
        self.payRate = payRate

    def __add__(self, otherSelf):
        sumOfPay = self.payRate + otherSelf.payRate
        return(sumOfPay)

    def printCheque(self, numberOfHoursWorked):
        if (numberOfHoursWorked > 40):
           grossIncome = (numberOfHoursWorked - 40) * (self.payRate * 2)
           grossIncome = grossIncome + (40 * self.payRate)           
        else:
            grossIncome = numberOfHoursWorked * self.payRate

        return super(HourlyEmployee, self).printCheque(grossIncome)


class SalaryEmployee(Employee):
    def __init__(self, lastName, firstName, salary):
        super(HourlyEmployee, self).__init__(lastName, firstName)
        self.salary = salary

    def __add__(self, otherSelf):
        sumOfPay = self.salary + otherSelf.salary
        return(sumOfPay)

    def printCheque(self):
        cheque = super(SalaryEmployee, self).printCheque(self.salary)
        # Do Additional Tax Stuff and Add Vacation Hours

执行上述操作应该有助于解决“我必须创建另一个类似于员工的类,但是这个人在一小时内得到薪水。我需要做的第一件事就是将payRate改为薪水。我是不知道这会是什么样子,我尝试了一些东西,但它们没有用。“

使用此类继承将使您能够根据员工类型更改属性。

此外,我尝试重构一下printCheque函数,以显示如何使用超级函数来帮助抽象出一些常见功能,然后在类级别上添加功能。我不太清楚所有打印功能是怎么回事。最好使用字符串格式并返回它以便根据不同的类实例操作它。

答案 1 :(得分:-1)

你可以在一堂课中完成所有这些工作。只需初始化方法中的对象而不是_init __。

class Employee():

    def __init__(self, firstName, lastName):
        self.firstName = firstName
        self.lastName = lastName

    def __repr__(self):
        pass

    def __addHourly__(self, payRate, extraPay):
        pass

    def __addSalary__(self, salary, extraPay):
        pass

    def printHourlyCheque(self, payRate, hoursWorked):
        pass

    def printSalaryCheque(self, Salary, vacationDays, sickDays):
        pass

然后,当您调用该类时,只需使用if语句来选择要调用的方法。

employee = Employee.repr()
status = some user input way to identify salary or hourly employee. 
    if status == 'Hourly':
        Employee.addHourly(payRate, extraPay)
        Employee.printHourlyCheque(payRate, #number of hours worked)
    elif status == 'Salary':
        Employee.adSalary
        Employee.printSalaryCheck(salary, #vacation days, #sick days)

代码有点粗略,但主要思想是在一个类中定义所有方法,然后在main中使用条件来调用每小时相关方法或与工资相关的方法。要执行此操作,请在方法中初始化对象,而不是在 init 中,除了名称之类的通用对象。