已知某company有员工30人,分三类:WeekWorker每周580¥,MonthWorker月薪每月2500 ¥ ,YearWorker年薪每年22000 ¥ 。 已知有Employee类,有成员函数earning()用于计算Employee一年的收入。在conmpany类中有Employee[]存放公司员工,成员函数salariesPay()计算并打印公司的所有一年薪水之和。 请编写类Company,Employee,WeekWorker,MonthWorker,YearWorker,假定公司三类员工各自的数量用随机数来模拟,打印该公司一年的薪水支出。
时间: 2024-02-17 08:04:41 浏览: 79
以下是代码实现:
```python
import random
class Employee:
def earning(self):
pass
class WeekWorker(Employee):
def __init__(self, salary=580 * 52):
self.salary = salary
def earning(self):
return self.salary
class MonthWorker(Employee):
def __init__(self, salary=2500 * 12):
self.salary = salary
def earning(self):
return self.salary
class YearWorker(Employee):
def __init__(self, salary=22000):
self.salary = salary
def earning(self):
return self.salary
class Company:
def __init__(self):
self.employees = []
self.generateEmployees()
def generateEmployees(self):
weekWorkerNum = random.randint(1, 10)
monthWorkerNum = random.randint(1, 10)
yearWorkerNum = random.randint(1, 10)
for i in range(weekWorkerNum):
self.employees.append(WeekWorker())
for i in range(monthWorkerNum):
self.employees.append(MonthWorker())
for i in range(yearWorkerNum):
self.employees.append(YearWorker())
def salariesPay(self):
sum = 0
for employee in self.employees:
sum += employee.earning()
print("The company's total salaries pay is: ", sum)
if __name__ == '__main__':
company = Company()
company.salariesPay()
```
在上述代码中,我们定义了三个员工类 WeekWorker、MonthWorker 和 YearWorker,它们分别对应每周、每月和每年的薪水收入。在 Company 类中,我们通过随机数生成了公司的员工数量,并将这些员工添加到了一个 employees 的列表中。最后,我们调用 salariesPay 函数计算并打印出公司的总薪水支出。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)