2014-11-25 18 views
-1

我有以下代码:我可以添加到继承函数吗?

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包括另一个税收,并且还显示休假时间。我是否可以在没有定义新功能的情况下将这样的功能添加到功能中,还是需要创建一个全新的功能?

不确定如何做到这两者中的任何一个,并且您可以给予的任何帮助都会有很大的帮助。

谢谢!

回答

0

我可以建议重构一点点...... 似乎小时工和雇员是两种不同类型的雇员,应该扩展一个基本的Employee类。

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函数来展示如何使用超级函数来帮助抽象掉一些常见功能,然后在类级别基础上添加功能。我不太清楚所有打印功能的情况。使用字符串格式并返回它可能会更好,以便根据不同的类实例对其进行操作。

+0

如何从第三个类继承out。是否仅仅从员工继承?或者我将不得不创建第三课。 – Dragonchicken 2014-11-25 04:14:09

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

的代码有点粗糙,但其主要思想是定义所有的方法在一个类中,然后使用条件在主调用无论是每小时相关的方法或工资相关的方法。为此,请在方法自己内部初始化对象,而不要在init中,但通用对象(如名称)除外。