【问题标题】:Call Superclass method from overridden Subclass method从重写的子类方法调用超类方法
【发布时间】:2017-07-07 17:29:25
【问题描述】:

我确信这有一个简单的解决方案,但我是 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);     
}


}

我很困惑!

【问题讨论】:

  • 如果可能的话,请发布你的两个完整的类,你可能在创建类的实例时没有初始化这些值。默认情况下,所有int 都得到0floatdouble 得到0.0
  • 你能扩展“当从同名方法调用它时”...
  • @OusmaneMahyDiaw OP 只想在覆盖它的方法中调用一个方法。
  • 他所做的并提到它在他这样做时也有效....
  • @jackjay - 使用完整的 Pay 类代码编辑了我的帖子

标签: java subclass superclass overriding


【解决方案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())时,它设置了CalPayrollPay继承的变量,而不是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();
        }

【讨论】:

  • 我的错,我以为您只想要 Pay 类中的代码,我已经使用接受用户输入以将值分配给初始化变量的代码编辑了原始帖子
  • 谢谢 JackJay,我明天去看看!
【解决方案2】:

请发布完整的代码。 似乎由于某种原因,您的超类方法的变量没有正确分配值。并且它们使用默认值进行初始化,这使所有内容都为 0。如果您粘贴完整的类,我将能够提供更好的帮助。

【讨论】:

    猜你喜欢
    • 2012-06-15
    • 1970-01-01
    • 1970-01-01
    • 2011-10-24
    • 2020-11-24
    • 1970-01-01
    • 2019-01-19
    • 2012-04-18
    相关资源
    最近更新 更多