题目要求
通过继承实现员工工资核算打印功能 父类:员工类( Employee ) 子类:部门经理类( Manager )、普通员工类(Worker) (1)部门经理工资=1000+单日工资*天数*等级(1.2)。 =>奖金+基本工资(2)普通员工工资=单日工资*天数*等级(1.0) =>基本工资 (3)员工属性:姓名,单日工资,工作天数 (4)员工方法(打印工资) (5) 普通员工及部门经理都是员工子类,需要重写打印工资方法。 (6)定义并初始化普通员工对象,调用打印工资方法输出工资,定义并初始化部门经理对象,调用打印工资方法输出工资
代码
Employee类
/**
* 通过继承实现员工工资核算打印功能
* 父类:员工类( Employee )
* 子类:部门经理类( Manager )、普通员工类(Worker)
* (1)部门经理工资=1000+单日工资*天数*等级(1.2)。 =>奖金+基本工资
* 2) 普通员工工资=单日工资*天数*等级(1.0) =>基本工资
* (3)员工属性:姓名,单日工资,工作天数
* (4)员工方法(打印工资)
* (5) 普通员工及部门经理都是员工子类,需要重写打印工资方法。
* (5)定义并初始化普通员工对象,调用打印工资方法输出工资,定义并初始化部门经理对象,调用打印工资方法输出工资
*/
public class Employee {
//属性
private String name;
private double daysal;
private int workDays;
private double grade;
//方法 构造器 getter和setter
public Employee(String name, double daysal, int workDays, double grade) {
this.name = name;
this.daysal = daysal;
this.workDays = workDays;
this.grade = grade;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getDaysal() {
return daysal;
}
public void setDaysal(double daysal) {
this.daysal = daysal;
}
public int getWorkDays() {
return workDays;
}
public void setWorkDays(int workDays) {
this.workDays = workDays;
}
public double getGrade() {
return grade;
}
public void setGrade(double grade) {
this.grade = grade;
}
// 方法 打印工资
public void printSal() {
System.out.println(name + " 工资=" + daysal * workDays * grade);
}
}
Manager类
public class Manager extends Employee {
//特有属性 奖金
private double bouns;
// 创建Manager 对象时 奖金多少不确定的 所以在构造器中不给bonus
//通过setBonus 给奖金
public Manager(String name, double daysal, int workDays, double grade) {
super(name, daysal, workDays, grade);
}
public double getBouns() {
return bouns;
}
public void setBouns(double bouns) {
this.bouns = bouns;
}
//重写父类的打印工资方法
@Override
public void printSal() {
//因为经理的工资和父类不一样 所以重写
System.out.println("经理 " + getName() + " 工资="
+ (bouns + getDaysal() * getWorkDays() * getGrade()));
}
}
Worker类
public class Worker extends Employee{
public Worker(String name, double salary, int day, double grade) {
super(name, salary, day, grade);
}
//重写printSal
//因为普通员工 和父类的工资输出情况一样所以自己调用父类的printSal()
@Override
public void printSal() {
System.out.print("普通员工 ");
super.printSal();
}
}
测试类
public class test {
public static void main(String[] args) {
Manager manage = new Manager("刘备",300,30,1.2);
//设置一个奖金
manage.setBouns(3000);
//打印经理的工资情况
manage.printSal();
Worker worker = new Worker("张飞",100,30,1.0);
worker.printSal();
}
}