Java假定要为某个公司编写雇员工资支付程序,这个公司有各种类型的雇员(Employee),不同类型的雇员按不同的方式支付工资(都是整数): (1)经理(Manager)——每月获得一份固定的工资 (2)销售人员(Salesman)——在基本工资的基础上每月还有销售提成 (3)一般工人(Worker)——则按他每月工作的天数计算工资 在Employee中提供函数getSalary()计算每个雇员一个月的工资,并在子类中重写。 在main函数中构造Employee的三个变量,分别指向Manager、Salesman、Worker的对象,调用getSalary函数,输出三个对象的工资 输入格式: 经理的月工资 销售人员的基本工资 销售人员的提成 工人的工作天数 工人每天的工资 输出格式: 经理的工资 销售人员的工资 工人的工资 输入样例: 在这里给出一组输入。例如: 12000 3000 5000 22 200 输出样例: 在这里给出相应的输出。例如: 12000 8000
时间: 2024-03-16 15:41:13 浏览: 106
java代码-使用java解决为公司编写雇员工资支付程序,三种雇员,getSalary函数的源代码
5星 · 资源好评率100%
以下是Java实现代码:
```
import java.util.Scanner;
abstract class Employee {
double salary;
public abstract double getSalary();
}
class Manager extends Employee {
double fixedSalary;
public Manager(double fixedSalary) {
this.fixedSalary = fixedSalary;
}
public double getSalary() {
salary = fixedSalary;
return salary;
}
}
class Salesman extends Employee {
double baseSalary;
double commission;
public Salesman(double baseSalary, double commission) {
this.baseSalary = baseSalary;
this.commission = commission;
}
public double getSalary() {
salary = baseSalary + commission;
return salary;
}
}
class Worker extends Employee {
int workDays;
double dailySalary;
public Worker(int workDays, double dailySalary) {
this.workDays = workDays;
this.dailySalary = dailySalary;
}
public double getSalary() {
salary = workDays * dailySalary;
return salary;
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double fixedSalary = scanner.nextDouble();
double baseSalary = scanner.nextDouble();
double commission = scanner.nextDouble();
int workDays = scanner.nextInt();
double dailySalary = scanner.nextDouble();
Employee manager = new Manager(fixedSalary);
Employee salesman = new Salesman(baseSalary, commission);
Employee worker = new Worker(workDays, dailySalary);
System.out.println((int)manager.getSalary() + " " + (int)salesman.getSalary() + " " + (int)worker.getSalary());
}
}
```
阅读全文