通过继承实现员工工资核算打印功能
父类:员工类
子类:部门经理类、普通员工类
(1)部门经理工资=1000+单日工资*天数*等级(1.2)。=>奖金+基本工资(2)普通员工工资=单日工资*天数*等级(1.0);=>基本工资
(3) 员工属性:姓名,单日工资,工作天数(4)员工方法(打印工资)
(5)普遍员工及部门经理都是员工子类,需要重写打印工资方法。
(5)定义并初始化普通员工对象,调用打印工资方法输出工资,定义并初始化部门经理对象,调用打印工资方法输出工资
Homeworkmain class
package com.hspedu.homework;
public class Homework {
public static void main(String[] args) {
Manager manager = new Manager("刘备", 100, 20, 1.2);
//设置奖金
manager.setBonus(3000);
//打印经理的工资情况
manager.printSal();
Worker worker = new Worker("关羽", 80, 100, 1.0);
worker.printSal();
}
}
Employee parent class
package com.hspedu.homework;
public class Employee {
//属性
private String name;
private double daySal;
private int workDays;
private double grade;
//方法[构造器]
//打印工资的方法
public void printSal(){
System.out.println(name + " 工资=" + daySal * workDays * grade);
}
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;
}
}
Manager subclass
package com.hspedu.homework;
public class Manager extends Employee{
//特有的属性
private double bonus;
//奖金通过set方法设置
public Manager(String name, double daySal, int workDays, double grade) {
super(name, daySal, workDays, grade);
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
//重写工资打印方法
@Override
public void printSal() {
//重写经理的工资计算方式
System.out.println("经理 " + getName() + " 工资是=" + (bonus +
getDaySal() * getWorkDays() * getGrade()));
}
}
Worker subclass
package com.hspedu.homework;
public class Worker extends Employee{
//普通员工没有特有的属性
public Worker(String name, double daySal, int workDays, double grade) {
super(name, daySal, workDays, grade);
}
//重写printSal
@Override
public void printSal() {
System.out.print("普通员工 ");
super.printSal();
}
}