创建父类
package com.ytzl.chapter04.wjt.one;
/**
* Create with IntelliJ IDEA.
* Description:
* User: 稚萱
* Author 86185
* Date: 2022-03-18
* Time: 14:21
*/
public abstract class Employee {
private String name;//名字
private int WorkNumber;//工号
private double bonus;//奖金
public Employee() {
}
public Employee(String name, int workNumber, double bonus) {
this.name = name;
WorkNumber = workNumber;
this.bonus = bonus;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getWorkNumber() {
return WorkNumber;
}
public void setWorkNumber(int workNumber) {
WorkNumber = workNumber;
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
//计算奖金的方法
public abstract double calcuBonus();
@Override
public String toString() {
return "Employee{" +
"我的名字为:='" + name + '\'' +
", 我的工号为:=" + WorkNumber +
", 我的工资为:=" + bonus +
'}';
}
}
员工类
package com.ytzl.chapter04.wjt.one;
/**
* Create with IntelliJ IDEA.
* Description:
* User: 稚萱
* Author 86185
* Date: 2022-03-18
* Time: 14:29
*/
public class OrdinaryEmployees extends Employee{
private double ordinaryEmployees=0.015;
public OrdinaryEmployees() {
}
public OrdinaryEmployees(String name, int workNumber, double bonus) {
super(name, workNumber, bonus);
}
public double getOrdinaryEmployees() {
return ordinaryEmployees;
}
public void setOrdinaryEmployees(double ordinaryEmployees) {
this.ordinaryEmployees = ordinaryEmployees;
}
@Override
public double calcuBonus() {
return super.getBonus()*getOrdinaryEmployees();
}
@Override
public String toString() {
System.out.println(super.toString());
return "OrdinaryEmployees{" +
"我的奖金为=" + ordinaryEmployees*super.getBonus() +
'}';
}
}
经理类
package com.ytzl.chapter04.wjt.one;
/**
* Create with IntelliJ IDEA.
* Description:
* User: 稚萱
* Author 86185
* Date: 2022-03-18
* Time: 14:29
*/
public class TheManager extends Employee{
private double TheManager=0.02;
public TheManager() {
}
public double getOrdinaryEmployees() {
return TheManager;
}
public void setOrdinaryEmployees(double TheManager) {
this.TheManager = TheManager;
}
public TheManager(double TheManager) {
this.TheManager = TheManager;
}
public TheManager(String name, int workNumber, double bonus) {
super(name, workNumber, bonus);
}
@Override
public double calcuBonus() {
return super.getBonus()*getOrdinaryEmployees();
}
@Override
public String toString() {
System.out.println(super.toString());
return "TheManager{" +
"我的奖金为:=" + TheManager*super.getBonus() +
'}';
}
}
老板类
package com.ytzl.chapter04.wjt.one;
/**
* Create with IntelliJ IDEA.
* Description:
* User: 稚萱
* Author 86185
* Date: 2022-03-18
* Time: 14:40
*/
public class Boss {
public double totalSalary(Employee[] employee){
double totalSalary=0;
for (int i = 0; i < employee.length; i++) {
double bonus = employee[i].calcuBonus() * 12;
double wages = employee[i].getBonus() * 12;
totalSalary+=bonus+wages;
}
return totalSalary;
}
}
测试类
package com.ytzl.chapter04.wjt.one;
/**
* Create with IntelliJ IDEA.
* Description:
* User: 稚萱
* Author 86185
* Date: 2022-03-18
* Time: 15:02
*/
public class Test {
public static void main(String[] args) {
OrdinaryEmployees pu1=new OrdinaryEmployees("王小王",1909144,500);
OrdinaryEmployees pu2=new OrdinaryEmployees("王小",1909144,900);
Boss boss = new Boss();
OrdinaryEmployees[] employees = new OrdinaryEmployees[2];
employees[0]=pu1;
System.out.println(pu1.toString());
employees[1]=pu2;
System.out.println(boss.totalSalary(employees));
}
}