15.(封装、继承、super ) 某公司的雇员分为以下若干类 Employee: 这是所有员工总的父类,属性: 员工的姓名,员工的生日月份。方法getSalary(intmonth) 根据参数月份来...

在此回答中,我将使用 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 类:SalariedEmployeeHourlyEmployeeSalesEmployeeBasePlusSalesEmployee

``` 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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在此回答中,我将使用 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值