【问题标题】:Trying to set values in a second class for overload methods in first class?试图在第二类中为第一类中的重载方法设置值?
【发布时间】:2015-03-13 02:40:30
【问题描述】:

我是 Java 新手,目前正在创建一个程序,我在其中使用重载方法计算各种金额(税款、总工资等),然后显示它。我已经完成了大部分工作(我认为),但现在我需要在同一个包中创建第二个类,在其中我为第一个程序设置值以查看它是否有效。我不知道在涉及重载方法时如何调用该方法,我尝试使用 get 和 set 但我不知道如何在重载方法中应用它。如果我的术语有问题,我深表歉意,我还是个新手!

package payrolllibrary;

public class PayrollLibrary {

       public static float calculatePay(float hours, float rates)
       {System.out.println("Pay: " + rates*hours);
       return 0;
       }

       public static float calculatePay(float hours, float rates, float multiplier)
        {System.out.println("Pay: " + rates*hours*multiplier);
       return 0;
       }      

       public static float calculateGrossPay(float regularPay, float overtimePay, float shift2Pay, float shift3Pay, float weeklyPay)
       {float grossPay;
       grossPay = regularPay + overtimePay + shift2Pay + shift3Pay + weeklyPay;
       System.out.println("Gross Pay: " + grossPay);
       return 0;
       }

       public static float calculatedSocialSecurityTax(float grossPay, float ytdEarnings, float ytdSocialSecurity)
       {
           float calculateSocialSecurityTax = 0;
           float calculateGrossPay = 0;

           if(ytdEarnings > 118500)
               {calculateSocialSecurityTax = 0;}
           if (ytdEarnings+calculateGrossPay < 118500)
               {calculateSocialSecurityTax = calculateGrossPay*.062f;}
           if (ytdEarnings+calculateGrossPay > 118500)
                {if (calculateGrossPay*.062 <= 118500)
                {calculateSocialSecurityTax = 118500;}               
                }
           {System.out.println("Social Security Tax: " + calculateSocialSecurityTax);
           return 0;
           }
       }

       public static float calculateMedicareTax(float grossPay)
       {float medicareTax;
       medicareTax = grossPay*.0145f;
       System.out.println("Medicare Tax: " + medicareTax);
       return 0;
       }

       public static float calculateStateTax(float grossPay, float stateWithholding)
       {System.out.println("State Tax: " + grossPay*stateWithholding);
       return 0;
       }

       public static float calculateNetPay(float grossPay, float federalWithholding, float socialSecurityWithholding, float medicareWithholding, float stateWithholding, float deduction1Amount, float deduction2Amount, float deduction3Amount)
       {float calculateNetPay;
       calculateNetPay = grossPay-federalWithholding-socialSecurityWithholding-medicareWithholding-stateWithholding-deduction1Amount-deduction2Amount-deduction3Amount;
       System.out.println("Net Pay: " + calculateNetPay);
       return 0;
       }

       public static float calculateDeduction(float grossPay, char deductionCode, float deductionValue)
       {if (deductionCode == 'N')
       {deductionValue = 0;}
       else if (deductionCode == 'F')
       {deductionValue = deductionValue;}
       else if (deductionCode == 'P')
       {deductionValue = deductionValue*grossPay;} 
       System.out.println("Deduction: " + deductionValue);
       return 0;
   }
}




package PayrollLibrary;

public class TestPayroll {

    public static void main(String[] args) {

        PayrollLibrary Person = new PayrollLibrary();
        Person.setRate(99.99);
        Person.setHours(99);
        Person.setMultiplier(999);
        Person.setRegularPay(999);
        Person.setOvertimePay(999);
        Person.setShift2Pay(999);
        Person.setShift3Pay(999);
        Person.setWeekendPay(999);
        Person.setYtdEarnings(999);
        Person.setYtdSocialSecurity(999);
        Person.setStateWithholding(999);
        Person.setDeduction1Amount(999);
        Person.setDeduction2Amount(999);
        Person.setDeduction3Amount(999);
        Person.setDeductionCode('F');
        Person.setDeductionAmount(999);


    }

}

【问题讨论】:

  • 你能贴出完整的PayrollLibrary 代码吗?您在 main 方法中调用的设置器在您的代码中不存在。

标签: java constructor overloading


【解决方案1】:

那些setXXX() 不会仅仅因为他们的英文意思是这样就设置值。它们只是方法的名称。要设置值,您必须对其进行编码。所以在你的PayrollLibrary 你应该有

public class PayrollLibrary {
    float rate;
    float hours;
    ......
    public void setRate(float inRate) {
        this.rate = inRate;
    }
    public void setHours(float inHours) {
        this.hours = inHours;
    }

那么在你的计算方法中,如果你要使用的是ratehours,你不必将它们传递给方法,简单的:

public float calculatePay() {
    return rate*hours;
}

所以在你的main() 中,这样做:

PayrollLibrary person = new PayrollLibrary();
person.setRate(99.99f);
person.setHours(99.0f);

要在上述setXXX 之后立即获得报酬,请执行以下操作:

float thePay = person.calculatePay();

请注意Java约定:类名以大写开头,变量和方法名以小写开头。

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多