gpt4 book ai didi

java - 从重写的子类方法调用父类(super class)方法

转载 作者:行者123 更新时间:2023-12-01 08:52:18 25 4
gpt4 key购买 nike

我确信这有一个简单的解决方案,但我是 Java 新手,无法解决它。

我有一个子类 Payroll,它扩展了父类(super class) Pay,它包含一个名为“calc_payroll”的重写方法。从这个方法中,我想调用同名的父类(super class)方法,并将输出分配给重写方法中的变量。我的代码如下

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;
}


}

下面是父类(super class)中 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;
}

当从不同的子类调用时,父类(super class)方法可以毫无问题地计算并返回总工资,但是当从具有相同名称的方法调用它时,上面代码中的打印行(我已标记为测试)显示所有变量的零

完整“付费”类别的代码如下所示

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);
}


}

我很困惑!

最佳答案

从您的代码中可以清楚地看出,ttlHours、stHours 和 Rate 未使用某个合理的值进行初始化。因此,当您仅调用 super.calc_Payroll() 时,将使用 0 或 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()) 时,它设置的是 CalPayrollPay 继承的变量,而不是 继承的变量>工资单类。您所要做的就是为 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();
}

关于java - 从重写的子类方法调用父类(super class)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42310619/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com