简体   繁体   English

无法弄清楚为什么抽象方法没有被覆盖

[英]Can't figure out why abstract method isn't overriding

My programming assignment tasked me with writing an increase/decreasePay abstract method that must be put in my abstract employee class.我的编程任务要求我编写一个增加/减少支付抽象方法,该方法必须放在我的抽象员工类中。 I can't seem to get the the method correct in HourlyWorker so that it will take increase or decrease the pay by a "percentage".我似乎无法在 HourlyWorker 中正确使用该方法,因此它将以“百分比”增加或减少工资。 My math is sound (monthly pay - or + (monthly pay * the percentage), but my output in my test class is coming out the same after increasing/decreasing pay. Any help?我的数学很不错(月薪 - 或 +(月薪 * 百分比),但是在增加/减少工资后,我在测试课上的输出结果是一样的。有帮助吗?

Employee class:员工类:

abstract public class Employee
{

private String lastName;
private String firstName;
private String ID;


public abstract void increasePay(double percentage);
public abstract void decreasePay(double percentage);
public abstract double getMonthlyPay();

public Employee(String last, String first, String ID)
   {
   lastName = last;
   firstName = first;
   this.ID = ID;
   } 

public void setLast(String last)
   {
      lastName = last;
   }
   
public void setFirst(String first)
   {
      firstName = first;
   }
   
public void setIdNumber(String ID)
   {
      this.ID = ID;
   }

      
public String getLastName()
{
   return lastName;
}

public String getFirstName()
{
   return firstName;
}

public String getName()
{
   return firstName + lastName;
}

public String getIdNumber()
{
   return ID;
}

} }


HourlyWorkerClass小时工类

public class HourlyWorker extends Employee
{
private int hours;
private double hourlyRate;
private double monthlyPay;

public HourlyWorker(String last, String first, String ID, double rate)
{
   super(last, first, ID);
   hourlyRate = rate;
}

public void setHours(int hours)
{
   this.hours = hours;
}

public int getHours()
{
   return hours;
}

public void setHourlyRate(double rate)
{
   if ( hours > 160 )
       this.hourlyRate = hourlyRate * 1.5;
    else 
       this.hourlyRate = rate;
}

public double getHourlyRate()
{
   return hourlyRate;
}


public void setMonthlyPay(double monthlyPay)
{
   monthlyPay = hourlyRate * hours;
}

public double getMonthlyPay()
{
   return hourlyRate * hours;
}

public void increasePay(double percentage)
{
   monthlyPay = monthlyPay* percentage;
}

public void decreasePay(double percentage)
{
   monthlyPay = monthlyPay* percentage;
}

public String toString() 
   {
        String result = "Name: " + getFirstName() + " " + getLastName() + "\nID: "
                         + getIdNumber() + " \nHourly Rate: " + hourlyRate;
                        return result;
   }

}

Testing class (currently testing increase测试类(目前测试增加

public class TestEmployee2
{
   public static void main(String[] args)
   {
   Employee [] staff = new Employee[3];
      Supervisor sup = new Supervisor("Boss", "Jim", "JB7865", 54000);
      HourlyWorker hw1 = new HourlyWorker("Bee", "Busy", "BB1265", 11.95);

      hw1.setHours(200);
            
      staff[0] = sup;
      staff[1] = hw1;        
      
   System.out.println(staff[0].getMonthlyPay());
   staff[0].increasePay(5);
   System.out.println(staff[0].getMonthlyPay());

   System.out.println(staff[1].getMonthlyPay());
   staff[1].increasePay(10);
   System.out.println(staff[1].getMonthlyPay());
   
}
}

Supervisor class:督导班:

public class Supervisor extends Employee
{
private double annualSalary;
private double monthlyPay;

public Supervisor(String last, String first, String ID, double salary)
{
   super(last, first, ID);
   annualSalary = salary;
}

public void setAnnualSalary(double salary)
{
   annualSalary = salary;
}

public double getAnnualSalary()
{
   return annualSalary;
}

public double getMonthlyPay()
{
   return ((annualSalary + (annualSalary * .02)) / 12);
}

public void increasePay(double percentage)
{
   monthlyPay = monthlyPay* percentage;
}

public void decreasePay(double percentage)
{
   monthlyPay = monthlyPay* percentage;
}

public String toString() 
   {
        String result = "Name: " + getFirstName() + " " + getLastName() + "\nID: "
                         + getIdNumber() + "\nAnnual Salary: " + annualSalary;
         return result;
   }
}

Output is:输出是:

4590.0 4590.0 2390.0 2390.0 4590.0 4590.0 2390.0 2390.0

Doesn't appear to be modifying getMonthlyPay()似乎没有修改 getMonthlyPay()

Should be:应该:

4590.00 4819.50 2390.00 2629.00 4590.00 4819.50 2390.00 2629.00

In increasePay , you are increasing monthlyPay :increasePay ,您增加了monthlyPay

public void increasePay(double percentage)
{
   monthlyPay = monthlyPay* percentage;
}

But when you getMonthlyPay , you calculate the pay using two other variables - hourlyRate and hours :但是当你getMonthlyPay ,你使用另外两个变量hourlyRatehours来计算工资:

public double getMonthlyPay()
{
   return hourlyRate * hours;
}

So changing monthlyPay doesn't affect what getMonthlyPay returns.因此,更改monthlyPay不会影响getMonthlyPay返回的内容。 I suspect a similar thing happens in Supervisor .我怀疑在Supervisor发生了类似的事情。

increasePay should instead increase hourlyRate : increasePay应该增加hourlyRate

public void increasePay(double percentage)
{
   hourlyRate *= 1 + percentage / 100;
}

Also, I don't think you need a monthlyPay field (or setMonthlyPay , for that matter) in HourlyEmployee at all.另外,我不认为你需要一个monthlyPay场(或setMonthlyPay中,对于这个问题) HourlyEmployee可言。 The monthly rate can always be calculated by hours and hourlyRate .月费率始终可以通过hourshourlyRate计算。


For Supervisor , do the same thing, and change annualSalary rather than monthlyPay :对于Supervisor ,做同样的事情,并改变annualSalary而不是monthlyPay

public double getMonthlyPay()
{
   return ((annualSalary + (annualSalary * .02)) / 12);
}

public void increasePay(double percentage)
{
   annualSalary *= 1 + percentage / 100;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM