某公司的雇员分为以下若干类: (1) Employee:这是所有员工总的父类 属性:员工的姓名,员工的生日月份 @方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过 生日,则公司会 额外奖励100元。 (2) SalariedEmployee: Employee 的子类,拿固定工资的员工 属性: 月薪。 (3)HourlyEmployee: Employee 的子类,按小时拿工资的员工,每月工作超 出160小时的部分按照1.5 倍工资发放。 属性:每小时的工资、每月工作的小时数 (4) SalesEmployee: Employee 的子类,销售,工资由月销售额和提成率决 定。 属性:月销售额、提成率 (5) BasePlusSalesEmployee: SalesEmployee 的子类,有固定底薪的销 售人员,工资由底薪加 上销售提成部分 属性: 底薪 要求: 创建一个Employee 数组,分别创建若干不同的Employee对象,并打 印某个月的工资。 注意:要求把每个类都做成完全封装,不允许非私有化属性。
时间: 2025-03-14 14:06:13 浏览: 84
### 设计思路
为了满足需求,可以采用面向对象编程的思想来构建 `Employee` 类以及其派生类。以下是具体的设计方案:
#### 1. 基础类定义 (`Employee`)
基础类 `Employee` 提供通用的属性和方法,例如姓名、ID 等基本信息,并声明抽象方法 `getSalary()` 来强制子类实现具体的工资计算逻辑。
```python
from abc import ABC, abstractmethod
class Employee(ABC):
def __init__(self, name: str, employee_id: int):
self.__name = name
self.__employee_id = employee_id
@property
def name(self):
return self.__name
@property
def employee_id(self):
return self.__employee_id
@abstractmethod
def getSalary(self) -> float:
"""返回该员工本月的薪资"""
pass
```
此部分实现了基本的封装原则,通过私有属性保护数据[^1]。
---
#### 2. 子类设计
##### (1) 工资固定型员工 (`SalariedEmployee`)
此类员工每月领取固定的薪水。
```python
class SalariedEmployee(Employee):
def __init__(self, name: str, employee_id: int, monthly_salary: float):
super().__init__(name, employee_id)
self.__monthly_salary = monthly_salary
def getSalary(self) -> float:
return self.__monthly_salary
```
此处利用父类构造函数初始化公共字段,并新增专属字段用于存储月收入。
---
##### (2) 小时工 (`HourlyEmployee`)
按小时支付报酬,需额外记录工作时间。
```python
class HourlyEmployee(Employee):
def __init__(self, name: str, employee_id: int, hourly_wage: float, hours_worked: float):
super().__init__(name, employee_id)
self.__hourly_wage = hourly_wage
self.__hours_worked = hours_worked
def getSalary(self) -> float:
overtime_hours = max(0, self.__hours_worked - 40)
regular_pay = min(self.__hours_worked, 40) * self.__hourly_wage
overtime_pay = overtime_hours * self.__hourly_wage * 1.5
return regular_pay + overtime_pay
```
这里考虑到了加班情况下的额外补偿机制。
---
##### (3) 销售员 (`SalesEmployee`)
销售员除了底薪外还享有提成奖励。
```python
class SalesEmployee(Employee):
def __init__(self, name: str, employee_id: int, base_salary: float, sales_amount: float, commission_rate: float):
super().__init__(name, employee_id)
self.__base_salary = base_salary
self.__sales_amount = sales_amount
self.__commission_rate = commission_rate / 100 # 转换为百分比形式
def getSalary(self) -> float:
return self.__base_salary + (self.__sales_amount * self.__commission_rate)
```
上述代码片段展示了如何基于销售额动态调整最终薪酬水平。
---
##### (4) 底薪加提成销售员 (`BasePlusSalesEmployee`)
这种类型的雇员既享受固定月薪又参与业绩分红。
```python
class BasePlusSalesEmployee(SalesEmployee):
def __init__(self, name: str, employee_id: int, base_salary: float, sales_amount: float, commission_rate: float, bonus: float):
super().__init__(name, employee_id, base_salary, sales_amount, commission_rate)
self.__bonus = bonus
def getSalary(self) -> float:
original_salary = super().getSalary()
return original_salary + self.__bonus
```
继承自 `SalesEmployee` 的方式简化了重复代码编写过程的同时增强了可维护性。
---
### 测试程序
下面是一个简单的测试脚本用来验证各个分类下实例的行为表现是否符合预期。
```python
if __name__ == "__main__":
employees = [
SalariedEmployee("Alice", 1, 5000),
HourlyEmployee("Bob", 2, 20, 45),
SalesEmployee("Charlie", 3, 3000, 10000, 10),
BasePlusSalesEmployee("David", 4, 3000, 10000, 10, 500)
]
for emp in employees:
print(f"{emp.name} ({type(emp).__name__}): ${emp.getSalary():,.2f}")
```
运行以上代码将会打印每位成员当期应得金额明细表单[^2]。
---
阅读全文
相关推荐



















