今天做一经典测试题
某公司的雇员分为以下若干类: Employee:这是所有员工总的父类,属性:员工的姓名,员工的生日月份。方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100元。 SalariedEmployee:Employee的子类,拿固定工资的员工。属性:月薪 HourlyEmployee:Employee的子类,按小时拿工资的员工,每月工作超出160小时的部分按照1.5倍工资发放。属性:每小时的工资、每月工作的小时数 SalesEmployee:Employee的子类,销售人员,工资由月销售额和提成率决定。属性:月销售额、提成率 BasePlusSalesEmployee:SalesEmployee的子类,有固定底薪的销售人员,工资由底薪加上销售提成部分。属性:底薪。 写一个程序,把若干各种类型的员工放在一个Employee数组里,写一个方法,打印出某月每个员工的工资数额。注意:要求把每个类都做成完全封装,不允许非私有化属性。
在写BasePlusSalesEmployee这个类时,因其继承SalesEmployee,所以当类bpse重写后调用getSalary方法时,调用的是SalesEmployee的getSalesEmployee的方法,而不是我想象中的Employee的方法,导致输出的工资结果错误,
在HourlyEmployee的getSalary方法中,因为没有写@Override,导致打出来的方法没有检查出是重写父类Employee的方法,在运行时直接运行了Employee的方法,导致工资不是100就是0.
吐了,这两个bug卡了我快一个小时,分开测试其工资计算方法,发现都没问题,第一个还情有可原,第二个纯属大意没长眼。
贴下代码
import java.util.Objects;
public class Employee {
private String name;
private int month;//员工生日月份
public Employee(String name, int month) {
this.name = name;
this.month = month;
}
public Employee() {
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Employee)) return false;
Employee employee = (Employee) o;
if (month != employee.month) return false;
return Objects.equals(name, employee.name);
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + month;
return result;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMonth() {
return this.month;
}
public void setMonth(int month) {
this.month = month;
}
// public double getSalary(int month){//当前月份
// if(this.month==month){
// return 100;
// }else {
// return 0;
// }
// }
public double getSalary(int dmonth){//当前月份
return (this.month==dmonth)?100:0;
}
public void view(int month,Employee e){
System.out.println(e.getName()+"您好,当前月份为"+month+"您的生日月份为"+e.getMonth());
System.out.println("所以您本月工资为:"+e.getSalary(month));
}
}
public class SalariedEmployee extends Employee {
private double yuexin;
public double getYuexin() {
return yuexin;
}
public void setYuexin(double yuexin) {
this.yuexin = yuexin;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof SalariedEmployee)) return false;
if (!super.equals(o)) return false;
SalariedEmployee that = (SalariedEmployee) o;
return Double.compare(that.yuexin, yuexin) == 0;
}
@Override
public int hashCode() {
int result = super.hashCode();
long temp;
temp = Double.doubleToLongBits(yuexin);
result = 31 * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public String toString() {
return "SalariedEmployee{" +
"yuexin=" + yuexin +
'}';
}
public SalariedEmployee(String name, int month, double yuexin) {
super(name, month);
this.yuexin = yuexin;
}
public SalariedEmployee(double yuexin) {
this.yuexin = yuexin;
}
@Override
public double getSalary(int dmonth) {//拿固定工资的员工。属性:月薪
double s=super.getSalary(dmonth)+getYuexin();
return s;
}
}
public class HourlyEmployee extends Employee{
private int hour;
private double hsales;
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
public double getHsales() {
return hsales;
}
public void setHsales(double hsales) {
this.hsales = hsales;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof HourlyEmployee)) return false;
if (!super.equals(o)) return false;
HourlyEmployee that = (HourlyEmployee) o;
if (hour != that.hour) return false;
return Double.compare(that.hsales, hsales) == 0;
}
@Override
public int hashCode() {
int result = super.hashCode();
long temp;
result = 31 * result + hour;
temp = Double.doubleToLongBits(hsales);
result = 31 * result + (int) (temp ^ (temp >>> 32));
return result;
}
public HourlyEmployee(String name, int month, int hour, double hsales) {
super(name, month);
this.hour = hour;
this.hsales = hsales;
}
public HourlyEmployee(int hour, double hsales) {
this.hour = hour;
this.hsales = hsales;
}
public HourlyEmployee(){
}
@Override
public double getSalary(int month){
//每月工作超出160小时的部分按照1.5倍工资发放。属性:每小时的工资、每月工作的小时数
double s1=(getHsales()<=160)?getHour()*getHsales():1.5*(getHour()-160)*getHsales()+160*getHsales();
double s=super.getSalary(month)+s1;
return s;
}
}
public class SalesEmployee extends Employee{
private double yuexsh;
private double ticheng;
public double getYuexsh() {
return yuexsh;
}
public void setYuexse(double yuexsh) {
this.yuexsh = yuexsh;
}
public double getTicheng() {
return ticheng;
}
public void setTicheng(double ticheng) {
this.ticheng = ticheng;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof SalesEmployee)) return false;
if (!super.equals(o)) return false;
SalesEmployee that = (SalesEmployee) o;
if (Double.compare(that.yuexsh, yuexsh) != 0) return false;
return Double.compare(that.ticheng, ticheng) == 0;
}
@Override
public int hashCode() {
int result = super.hashCode();
long temp;
temp = Double.doubleToLongBits(yuexsh);
result = 31 * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(ticheng);
result = 31 * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public String toString() {
return "SalesEmployee{" +
"yuexse=" + yuexsh +
", ticheng=" + ticheng +
'}';
}
public SalesEmployee(String name, int month, double yuexsh, double ticheng) {
super(name, month);
this.yuexsh = yuexsh;
this.ticheng = ticheng;
}
public SalesEmployee(double yuexsh, double ticheng) {
this.yuexsh = yuexsh;
this.ticheng = ticheng;
}
public SalesEmployee() {
}
@Override
public double getSalary(int month){//当前月份
// 销售人员,工资由月销售额和提成率决定。属性:月销售额、提成率
double s1=getTicheng()*getYuexsh();
double s=super.getSalary(month)+s1;
return s;
}
}
public class BasePlusSalesEmployee extends SalesEmployee{
private double dixin;
public double getDixin() {
return dixin;
}
public void setDixin(double dixin) {
this.dixin = dixin;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof BasePlusSalesEmployee)) return false;
if (!super.equals(o)) return false;
BasePlusSalesEmployee that = (BasePlusSalesEmployee) o;
return Double.compare(that.dixin, dixin) == 0;
}
@Override
public int hashCode() {
int result = super.hashCode();
long temp;
temp = Double.doubleToLongBits(dixin);
result = 31 * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public String toString() {
return "BasePlusSalesEmployee{" +
"dixin=" + dixin +
'}';
}
public BasePlusSalesEmployee(String name, int month, double yuexsh, double ticheng, double dixin) {
super(name, month, yuexsh, ticheng);
this.dixin = dixin;
}
public BasePlusSalesEmployee(double yuexse, double ticheng, double dixin) {
super(yuexse, ticheng);
this.dixin = dixin;
}
public BasePlusSalesEmployee(double dixin) {
this.dixin = dixin;
}
public BasePlusSalesEmployee() {
}
@Override
public double getSalary(int month) {//有固定底薪的销售人员,工资由底薪加上销售提成部分。属性:底薪
SalesEmployee sa=new SalesEmployee();
double s1=getTicheng()*getYuexsh()+getDixin();
double s=sa.getSalary(month)+s1;
return s;
}
}
public class Test { public static void main(String[] args) { SalariedEmployee sde=new SalariedEmployee("张三",5,5000);//5000 HourlyEmployee hoe=new HourlyEmployee("李四",6,150,50);//7600 BasePlusSalesEmployee bpse=new BasePlusSalesEmployee("王五",10,50000,0.1,2000);//7000 SalesEmployee se=new SalesEmployee("林六",6,60000,0.1);//6000 Employee e1=sde; Employee e2=hoe; Employee e3=bpse; Employee e4=se; Employee[] Employee={e1,e2,e3,e4}; int month=5; for (Employee ee:Employee) { ee.view(month, ee); }