本文主要是介绍4.通过继承实现员工工资核算打印功能Homework04.java,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
4.通过继承实现员工工资核算打印功能Homework04.java
父类:员工类(Employee)
子类:部门经理类(Manager)、普通员工类(Worker)
(1)部门经理工资=1000+单日工资天数等级(1.2)。=>奖金+基本工资(2)普通员工工资=单日工资天数等级(1.0) ; =>基本工资
(3)员工属性:姓名,单日工资,工作天数
(4)员工方法(打印工资)
(5)普遍员工及部门经理都是员工子类,需要重写打印工资方法。
(5)定义并初始化普通员工对象,调用打印工资方法输出工资,定义并初始化部门经理对象调用打印工资方法输出工资
public class Main {public static void main(String[] args) {Employee[] employees = new Employee[2];employees[0] = new Worker("张三",1234,30);employees[1] = new Manager("李四",2345,30);//employees[2] = new Manager("赵五",121,30);for (int i = 0; i < employees.length; i++) {if (employees[i] instanceof Worker) {employees[i].show();} else if (employees[i] instanceof Manager) {employees[i].show();}else {System.out.println("错误");}}}
}class Employee {private String name;private double todaySalary;//当日工资private int workDays;//工作天数public Employee(String name, double todaySalary, int workDays) {this.name = name;this.todaySalary = todaySalary;this.workDays = workDays;}public void show() {}public String getName() {return name;}public void setName(String name) {this.name = name;}public double getTodaySalary() {return todaySalary;}public void setTodaySalary(double todaySalary) {this.todaySalary = todaySalary;}public int getWorkDays() {return workDays;}public void setWorkDays(int workDays) {this.workDays = workDays;}
}class Worker extends Employee{private double salary;//月工资private double payScale = 1.0;public Worker(String name, double todaySalary, int workDays) {super(name, todaySalary, workDays);}@Overridepublic void show() {this.salary = this.getTodaySalary() * payScale * this.getWorkDays();System.out.println(salary);}
}
class Manager extends Employee{private double salary;//月工资private double payScale = 1.2;//工资等级public Manager(String name, double todaySalary, int workDays) {super(name, todaySalary, workDays);}@Overridepublic void show() {this.salary = 1000 + this.getTodaySalary() * payScale * this.getWorkDays();System.out.println(salary);}
}
编译结果
这篇关于4.通过继承实现员工工资核算打印功能Homework04.java的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!