所以我在这个薪资类上工作,我需要创建两个员工,工作时间和小时工资,并计算工资。最后,我必须为先前创建的一名员工增加10个额外小时,并计算和显示总薪资。我写了两个类,一切工作都很好,但是当我查看工资总额时,并没有考虑增加的时间。
增加小时后,totalPayRoll的输出应为$ 2000,但我仍然可以得到$ 1750!

public class PayRoll {

static double  getTotalPayRoll()
{
    return TotalPayRoll;

}


 public String employeeId;
 public int hoursWorked;
 public final double hourlyPay;
 private static double TotalPayRoll;
 private static double Salary;



 public PayRoll (String theEmployeeId, int theHoursWorked,
                     double theHourlyPay)
 {
    this.employeeId = theEmployeeId;
    this.hoursWorked = theHoursWorked;
    this.hourlyPay = theHourlyPay;

    Salary = hoursWorked*hourlyPay;
    TotalPayRoll = TotalPayRoll + Salary ;

 }


 public String getTheEmployeeId()
 {
     return this.employeeId;
 }

 public int getTheHoursWorked()
 {
     return this.hoursWorked;
 }

 public double getTheHourlyPay()
 {
     return this.hourlyPay;
 }




 public double getSalary()
 {
     return PayRoll.Salary;
 }

 public void increase (int extraHours)
 {
     hoursWorked = (hoursWorked + extraHours);
 }


 public void changeTheHoursWorked (int amount)
 {
     hoursWorked = hoursWorked + amount;
 }

 public void calculateSalary()
 {
     Salary = hoursWorked*hourlyPay;
 }

 public void calculateTotalPayRoll()
 {
     TotalPayRoll= TotalPayRoll+Salary;
 }

  public void changeHours(int newHours)
  {
      hoursWorked = newHours;
  }



}


这是主要的

public class Test {


    public static void main(String[] args) {
        // TODO code application logic here

        Date d = new Date();
        DateFormat df = DateFormat.getDateInstance( DateFormat.MEDIUM );
        NumberFormat nf = NumberFormat.getCurrencyInstance();

        System.out.println("\nPayroll For Week Ending " + df.format(d));
        System.out.println("-------------------------------------");
        PayRoll employee1 = new PayRoll("444-4444", 30, 25);
        employee1.calculateSalary();
        displaySalary(employee1, nf);

        PayRoll employee2 = new PayRoll("555-5555", 20, 50);
        employee2.calculateSalary();
        displaySalary(employee2, nf);

        System.out.println("Increase " + employee1.getTheEmployeeId() +
                              " by 10 hours");
        employee1.changeTheHoursWorked(10); // 10 hours increase
        employee1.calculateSalary();
        displaySalary(employee1, nf);

        System.out.println("Total payout amount.. " +
                             nf.format(PayRoll.getTotalPayRoll()));

      }
      public static void displaySalary(PayRoll e, NumberFormat nf)
      {
           System.out.println("Employee #: " + e.getTheEmployeeId());
           System.out.println("Hours Worked: " + e.getTheHoursWorked());
           System.out.println("Hourly Rate: " + e.getTheHourlyPay());
           System.out.println("Your Salary is: " + e.getSalary());


           System.out.println("---------------------------------\n");
       }

      }

最佳答案

在您的课程中:

private static double TotalPayRoll;
 private static double Salary;


两者都是静态成员(类级别成员),因此这些成员只有一个副本,这些副本将在所有对象之间共享。因为对于不同的工资单,TotalPayRoll和薪水应该不同,所以这些应该是非静态的。

10-06 00:22