在此回答中,我将使用 Python 语言来给出示例代码。
首先,我们需要定义一个名为 Employee
的父类,该类具有两个属性:姓名和生日月份,以及一个方法 getSalary()
。
class Employee:
def __init__(self, name, birthday_month):
self.__name = name
self.__birthday_month = birthday_month
def getSalary(self, month):
salary = 0
if month == self.__birthday_month:
salary += 100
return salary
然后,我们可以定义四个子类来继承 Employee
类:SalariedEmployee
、HourlyEmployee
、SalesEmployee
和 BasePlusSalesEmployee
。
``` class SalariedEmployee(Employee): def init(self, name, birthday_month, salary): super().init(name, birthday_month) self.__salary = salary
def getSalary(self, month):
return super().getSalary(month) + self.__salary
class HourlyEmployee(Employee): def init(self, name, birthday_month, hourly_wage, hours_worked): super().init(name, birthday_month) self.__hourly_wage = hourly_wage self.__hours_worked = hours_worked
def getSalary(self, month):
salary = super().getSalary(month)
overtime_hours = max(0, self.__hours_worked - 160)
salary += overtime_hours * self.__hourly_wage * 1.5
salary += (self.__hours_worked - overtime_hours) * self.__hourly_wage
return salary
class SalesEmployee(Employee): def init(self, name, birthday_month, sales, commission_rate): super().init(name, birthday_month) self.__sales = sales self.__commission_rate = commission_rate
def getSalary(self, month):
return super().getSalary(month) + self.__sales * self.__commission_rate
class BasePlusSalesEmployee(SalesEmployee): def init(self, name, birthday_month, sales, commission_rate, base_salary): super().init(name, birthday_month, sales, commission_rate) self.__base_salary = base_salary
def getSalary(self, month):
return super().getSalary(month) + self.__base_salary