[英]Can't figure out why abstract method isn't overriding
我的编程任务要求我编写一个增加/减少支付抽象方法,该方法必须放在我的抽象员工类中。 我似乎无法在 HourlyWorker 中正确使用该方法,因此它将以“百分比”增加或减少工资。 我的数学很不错(月薪 - 或 +(月薪 * 百分比),但是在增加/减少工资后,我在测试课上的输出结果是一样的。有帮助吗?
员工类:
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;
}
}
小时工类
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;
}
}
测试类(目前测试增加
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());
}
}
督导班:
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;
}
}
输出是:
4590.0 4590.0 2390.0 2390.0
似乎没有修改 getMonthlyPay()
应该:
4590.00 4819.50 2390.00 2629.00
在increasePay
,您增加了monthlyPay
:
public void increasePay(double percentage)
{
monthlyPay = monthlyPay* percentage;
}
但是当你getMonthlyPay
,你使用另外两个变量hourlyRate
和hours
来计算工资:
public double getMonthlyPay()
{
return hourlyRate * hours;
}
因此,更改monthlyPay
不会影响getMonthlyPay
返回的内容。 我怀疑在Supervisor
发生了类似的事情。
increasePay
应该增加hourlyRate
:
public void increasePay(double percentage)
{
hourlyRate *= 1 + percentage / 100;
}
另外,我不认为你需要一个monthlyPay
场(或setMonthlyPay
中,对于这个问题) HourlyEmployee
可言。 月费率始终可以通过hours
和hourlyRate
计算。
对于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.