【发布时间】:2014-11-25 01:13:06
【问题描述】:
我有以下代码:
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.
我必须创建另一个类似于 Employee 的 Class,但是这个人的薪水超过了按小时支付的工资。我需要做的第一件事是将 payRate 更改为薪水。我不确定这会是什么样子,我尝试了一些方法,但没有奏效。
我需要做的另一件事是更改 printCheque 以包含其他税款,并显示假期时间。我是否可以在不定义新函数的情况下将类似的内容添加到函数中,还是必须创建一个全新的函数?
不太确定如何执行其中任何一项,您可以提供的任何帮助都会有很大帮助。
谢谢!
【问题讨论】:
标签: python class inheritance overriding