我确信这有一个简单的解决方案,但我是Java的新手,无法解决这个问题。
我有一个扩展超类Pay的子类Payroll,它包含一个名为' calc_payroll'的重写方法。从这个方法,我想调用相同名称的超类方法,并将输出分配给重写方法中的变量。我的代码在
之下public class Payroll extends Pay
{
public double calc_Payroll()
{
double grossPay = super.calc_Payroll();
double taxAmt = tax(grossPay);
double netPay = grossPay - taxAmt;
System.out.println(grossPay);
return netPay;
}
}
以下是超类
中calc_payroll方法的代码public double calc_Payroll()
{
double otRate = rate * 1.77;
double otHours = ttlHours - stHours;
if(stHours == 0)
{
grossPay = otHours * rate;
}
else
{
grossPay = ((stHours * rate) + (otHours * otRate));
}
System.out.println(stHours + "//" + otHours + "//" + rate);//for testing
return grossPay;
}
超类方法函数没有问题来计算并返回从不同子类调用时的总工资,但是当从具有相同名称的方法调用它时,上面代码中的打印行(我已标记为测试)显示所有变量的零
完整代码'支付'课程低于要求
public class Pay
{
private double ttlHours;
private int stHours;
private double rate;
double grossPay = 0.0;
final double TAXL = 0.07;
final double TAXM = 0.1;
final double TAXH = 0.16;
public void SetHours(double a)
{
ttlHours = a;
}
public void SetHoursStr(int a)
{
stHours = a;
}
public void SetRate(double a)
{
rate = a;
}
public double GetHours()
{
return ttlHours;
}
public int GetStHours()
{
return stHours;
}
public double GetRate()
{
return rate;
}
public double taxRate()
{
double taxRate = 0.0;
if(grossPay <= 399.99)
{
taxRate = TAXL;
}
else if(grossPay <= 899.99)
{
taxRate = TAXM;
}
else
{
taxRate = TAXH;
}
return taxRate;
}
public double tax(double grossPay)
{
double ttlTax = 0.0;
if(grossPay < 400.00)
{
ttlTax += (grossPay * TAXL);
}
else if(grossPay < 900.00)
{
ttlTax += (grossPay * TAXM);
}
else
{
ttlTax += (grossPay * TAXH);
}
return ttlTax;
}
public double calc_Payroll()
{
double otRate = rate * 1.77;
double otHours = ttlHours - stHours;
if(stHours == 0)
{
grossPay = otHours * rate;
}
else
{
grossPay = ((stHours * rate) + (otHours * otRate));
}
System.out.println(stHours + "//" + otHours + "//" + rate);//for testing
return grossPay;
}
}
子类Payroll不包含其他代码
以下是接受用户输入以将值分配给初始化变量
的代码public class CalPayroll extends Pay
{
Payroll nPay = new Payroll();
Accept Read = new Accept();
public void AcceptPay()
{
char select = '0';
while(select != 'e' && select != 'E')
{
System.out.println("Payroll Computation \n");
System.out.print("Enter number of hours worked (00.0) <0 for Quick exit>: ");
SetHours(Read.AcceptInputDouble());
System.out.print("Enter first number of hours straight (integer or 0 to disable): ");
SetHoursStr(Read.AcceptInputInt());
System.out.print("Enter hourly rate of worker (00.00): ");
SetRate(Read.AcceptInputDouble());
Screen.ScrollScreen('=', 66, 1);
Screen.ScrollScreen(1);
displayInfo();
System.out.println("e to exit, any other letter + <Enter> to continue");
select = Read.AcceptInputChar();
}
}
public void displayInfo()
{
NumberFormat currency = NumberFormat.getCurrencyInstance();
NumberFormat percent = NumberFormat.getPercentInstance();
System.out.println("Gross pay is :" + currency.format(calc_Payroll()));
System.out.println("Tax is :" + percent.format(taxRate()));
System.out.println("Net pay is :" + currency.format(nPay.calc_Payroll()));
Screen.ScrollScreen(1);
}
}
我很困惑!
答案 0 :(得分:1)
从您的代码中可以清楚地看出ttlHours, stHours and rate
没有以合理的价值进行初始化。因此,当您拨打super.calc_Payroll()
时,我会使用0 or 0.0
之类的值,就像我在评论中所解释的那样。在调用super.calc_Payroll()
之前,首先设置这些变量的值是很好的。
SetHours(23.4); //some value
SetHoursStr(5); //some value
SetRate(2.3); //some value
此外,您还没有Pay
类的构造函数,尝试制作它并在构造函数中初始化所有未初始化的变量,或使用setter/getter
方法来设置和获取值。
由于您的两个类都扩展了Pay
类,因此会产生您所面临的问题。当您致电SetHours(Read.AcceptInputDouble())
时,它会设置CalPayroll
从Pay
继承的变量,而不是Payroll
类继承的变量。您需要做的是为Payroll
实例以及当前类设置变量,因为它们都扩展为Pay
。执行以下操作将while
循环替换为
while(select != 'e' && select != 'E')
{
System.out.println("Payroll Computation \n");
System.out.print("Enter number of hours worked (00.0) <0 for Quick exit>: ");
SetHours(Read.AcceptInputDouble());
nPay.SetHours(GetHours());
System.out.print("Enter first number of hours straight (integer or 0 to disable): ");
SetHoursStr(Read.AcceptInputInt());
nPay.SetHoursStr(GetStHours());
System.out.print("Enter hourly rate of worker (00.00): ");
SetRate(Read.AcceptInputDouble());
nPay.SetRate(GetRate());
Screen.ScrollScreen('=', 66, 1);
Screen.ScrollScreen(1);
displayInfo();
System.out.println("e to exit, any other letter + <Enter> to continue");
select = Read.AcceptInputChar();
}
答案 1 :(得分:0)
请发布完整的代码。 似乎由于某种原因,超类方法的变量没有正确地赋值。并且它们使用默认值初始化,这使得所有内容都为0.如果您粘贴完整的类,我将能够提供更好的帮助。