使用JAVA的继承与封装技术实现员工工资计算!
父类
1 /** 2 * 父类 3 * @author Administrator 4 * 5 */ 6 public class Employee { 7 8 private String name;//姓名 9 private int month;//生日月份 10 11 public String getName() { 12 return name; 13 } 14 public void setName(String name) { 15 this.name = name; 16 } 17 public int getMonth() { 18 return month; 19 } 20 public void setMonth(int month) { 21 this.month = month; 22 } 23 24 public double getSalary(int month){ 25 double gz=0; 26 if(this.getMonth()==month){ 27 gz+=100; 28 } 29 return gz; 30 } 31 }
固定工资
1 /** 2 * 固定工资 3 * @author Administrator 4 * 5 */ 6 public class SalariedEmployee extends Employee { 7 private double yx;//月薪 8 9 public double getYx() { 10 return yx; 11 } 12 13 public void setYx(double yx) { 14 this.yx = yx; 15 } 16 17 public double getSalary(int month){ 18 double gz=0; 19 if(this.getMonth()==month){ 20 gz+=100; 21 } 22 return gz+this.getYx(); 23 } 24 }
按小时拿工资
1 /** 2 * 按小时拿工资 3 * @author Administrator 4 * 5 */ 6 public class HourlyEmployee extends Employee { 7 private double gs;//工时 8 private double sx;//时薪 9 public double getGs() { 10 return gs; 11 } 12 public void setGs(double gs) { 13 this.gs = gs; 14 } 15 public double getSx() { 16 return sx; 17 } 18 public void setSx(double sx) { 19 this.sx = sx; 20 } 21 22 public double getSalary(int month){ 23 double gz=0; 24 if(this.getMonth()==month){ 25 gz+=100; 26 } 27 if(this.gs>160){ 28 gz+=160*this.getSx()+(this.getGs()-160)*this.getSx()*1.5; 29 }else{ 30 gz+=this.getSx()*this.getGs(); 31 } 32 return gz; 33 } 34 }
销售额与提成率
1 /** 2 * 销售额与提成率 3 * @author Administrator 4 * 5 */ 6 public class SalesEmployee extends Employee { 7 private double yxse;//月销售额 8 private double tcl;//提成率 9 public double getYxse() { 10 return yxse; 11 } 12 public void setYxse(double yxse) { 13 this.yxse = yxse; 14 } 15 public double getTcl() { 16 return tcl; 17 } 18 public void setTcl(double tcl) { 19 this.tcl = tcl; 20 } 21 22 public double getSalary(int month){ 23 double gz=0; 24 if(this.getMonth()==month){ 25 gz+=100; 26 } 27 return gz+this.getTcl()*this.getYxse(); 28 } 29 30 }
底薪加销售额
1 /** 2 * 底薪加销售额 3 * @author Administrator 4 * 5 */ 6 public class BasePlusSalesEmployee extends SalesEmployee{ 7 private double dx;//底薪 8 9 public double getDx() { 10 return dx; 11 } 12 13 public void setDx(double dx) { 14 this.dx = dx; 15 } 16 17 public double getSalary(int month){ 18 double gz=0; 19 if(this.getMonth()==month){ 20 gz+=100; 21 } 22 return gz+this.getDx()+this.getTcl()*this.getYxse(); 23 } 24 }
测试类
1 /** 2 * 测试类 3 * @author Administrator 4 * 5 */ 6 public class Test { 7 public static void main(String[] args) { 8 9 int month=4; 10 11 SalariedEmployee g=new SalariedEmployee(); 12 13 g.setName("小王"); 14 g.setMonth(3); 15 g.setYx(5000); 16 System.out.println(g.getName()+month+"月工资为"+g.getSalary(month)); 17 18 HourlyEmployee s=new HourlyEmployee(); 19 20 s.setName("小张"); 21 s.setMonth(5); 22 s.setSx(10); 23 s.setGs(160); 24 System.out.println(s.getName()+month+"月工资为"+s.getSalary(month)); 25 26 SalesEmployee t=new SalesEmployee(); 27 28 t.setName("小二"); 29 t.setMonth(6); 30 t.setYxse(100000); 31 t.setTcl(0.05); 32 System.out.println(t.getName()+month+"月工资为"+t.getSalary(month)); 33 34 BasePlusSalesEmployee d=new BasePlusSalesEmployee(); 35 d.setName("小明"); 36 d.setMonth(7); 37 d.setYxse(10000); 38 d.setTcl(0.02); 39 d.setDx(3000); 40 System.out.println(d.getName()+month+"月工资为"+d.getSalary(month)); 41 42 43 } 44 }