已知某公司有三种类型的员工,每个员工都有自己的工号、姓名,计算发放工资总数用类的知识解决
时间: 2024-03-18 14:38:43 浏览: 28
好的,根据你的问题,我可以提供以下的类设计:
```
class Employee:
def __init__(self, emp_id, name):
self.emp_id = emp_id
self.name = name
def calculate_salary(self):
pass
class FullTimeEmployee(Employee):
def __init__(self, emp_id, name, salary):
super().__init__(emp_id, name)
self.salary = salary
def calculate_salary(self):
return self.salary
class PartTimeEmployee(Employee):
def __init__(self, emp_id, name, hours_worked, hourly_rate):
super().__init__(emp_id, name)
self.hours_worked = hours_worked
self.hourly_rate = hourly_rate
def calculate_salary(self):
return self.hours_worked * self.hourly_rate
class Contractor(Employee):
def __init__(self, emp_id, name, hours_worked, hourly_rate, overtime_hours, overtime_rate):
super().__init__(emp_id, name)
self.hours_worked = hours_worked
self.hourly_rate = hourly_rate
self.overtime_hours = overtime_hours
self.overtime_rate = overtime_rate
def calculate_salary(self):
return self.hours_worked * self.hourly_rate + self.overtime_hours * self.overtime_rate
```
这里我们定义了一个基本的 `Employee` 类,其包含了每个员工都应该有的工号和姓名属性,并且定义了一个 `calculate_salary` 方法,用于计算员工的工资。然后,我们派生出了三个子类,分别代表全职员工、兼职员工和合同工。每个子类都重写了 `calculate_salary` 方法,根据自己的特点计算工资。
使用时,我们可以分别创建不同类型的员工实例,然后调用他们的 `calculate_salary` 方法来计算他们的工资总数。
举个例子:
```
# 创建全职员工实例
ft_employee = FullTimeEmployee("001", "张三", 10000)
# 创建兼职员工实例
pt_employee = PartTimeEmployee("002", "李四", 80, 150)
# 创建合同工实例
ct_employee = Contractor("003", "王五", 100, 120, 10, 200)
# 计算工资总数
total_salary = ft_employee.calculate_salary() + pt_employee.calculate_salary() + ct_employee.calculate_salary()
print("工资总数:", total_salary)
```
输出结果:
```
工资总数: 16600
```
以上就是一个简单的类设计,用于计算发放工资总数。希望能够帮助到你!