员工类有一个扭曲

时间:2014-02-16 21:07:09

标签: java interface abstract

我们希望在执行应用程序时在屏幕上显示付费,但所有显示的是,即使我们在代码中进行了计算,我们也只能得到:

  

收入:0.0

正如您所看到的,其他类中有一些元素,但没有后顾之忧,因为除了HourlyEmployee类之外它们的工作方式与我们预期的一样。

HourlyEmployee类:

 class HourlyEmployee extends Employee 
{
   private double wage; // wage per hour
   private double hours;// hours worked for week
   private double earnings= hours *wage;

   // five-argument constructor
   public HourlyEmployee( String first, String last,double payMe,
      double hourlyWage, double hoursWorked )
   {
      super( first, last );
      setWage( hourlyWage ); // validate and store hourly wage
      setHours( hoursWorked ); // validate and store hours worked
       payMe = earned();
   } // end five-argument HourlyEmployee constructor

   // set wage
   public void setWage( double hourlyWage )
   {
      wage = ( hourlyWage < 0.0 ) ? 0.0 : hourlyWage;
   } // end method setWage

   // return wage
   public double getWage()
   {
      return wage;
   } // end method getWage

   // set hours worked
   public void setHours( double hoursWorked )
   {
      hours = ( ( hoursWorked >= 0.0 ) && ( hoursWorked <= 120.0 ) ) ?
         hoursWorked : 0.0;
   } // end method setHours

   // return hours worked
   public double getHours()
   {
      return hours;
   } // end method getHours

   // calculate earnings; override abstract method earnings in Employee

   public double earned()
   {
      if ( getHours() <= 40 ) // no overtime
         return getWage() * getHours();
      else
         return 40 * getWage() + ( getHours() - 40 ) * getWage() * 1.5;
   } // end method earnings

   @Override
   public double getPaymentAmount()
   {
       return earnings;
    }

   // return String representation of HourlyEmployee object
   @Override
   public String toString()
   {
      return String.format( "hourly employee: %s\n%s: $%f\n %s: %f", 
         super.toString(), "hourly wage", getWage(), 
         "hours worked", getHours() );
   } // end method toString
} // end class HourlyEmployee

测试类:

public class Test 
{
   public static void main( String args[] ) 
   {
      // create subclass objects                                          
      MonthlyEmployee salariedEmployee =                                 
         new MonthlyEmployee( "John", "Smith", 800.00 );  

      HourlyEmployee commissionEmployee = new HourlyEmployee("Sue", "Jones", 10000,        23.9, 5.7 ); 

      MonthlyEmployee worker3 = new MonthlyEmployee("Oliver" , "Queen" , 12500);


      System.out.println( "Employees processed individually:\n" );
      System.out.printf( "%s\n%s: %f\n\n",salariedEmployee, "earned",     salariedEmployee.getPaymentAmount() );

         System.out.printf("%s\n%s : $%f\n]n" , worker3 , "earned" ,   worker3.getPaymentAmount());


      System.out.printf( "%s\n%s: %f\n\n",commissionEmployee, "earned",     commissionEmployee.getPaymentAmount());


      // create four-element Employee array
      Employee employees[] = new Employee[ 3 ]; 

      // initialize array with Employees          
      employees[ 0 ] = salariedEmployee;          

      employees[ 1 ] = commissionEmployee; 

      employees [2] = worker3;


      System.out.println( "Employees processed polymorphically:\n" );

      // generically process each element in array employees
      // end for

      // get type name of each object in employees array
      for ( int j = 0; j < employees.length; j++ )      
         System.out.printf( "Employee %d is a %s\n", j, 
            employees[ j ].getClass().getName() );      
   } // end main
} // end class PayrollSystemTest

1 个答案:

答案 0 :(得分:3)

private double earnings= hours *wage;

这与excel不同,不要指望每次hourswage更改时都会重新计算该值。

因为double的默认值是0.0,这就是earnings等于0.0的原因。

初始化hourswage后,您必须初始化此值,很可能是在构造函数中:

public HourlyEmployee( String first, String last,double payMe,
      double hourlyWage, double hoursWorked ){
      super( first, last );
      setWage( hourlyWage ); // validate and store hourly wage
      setHours( hoursWorked ); // validate and store hours worked
      payMe = earned();
      earnings= hours *wage; 
}
  

但我正在努力,那就是价值   每次小时或工资变化重新计算

根据评论中的建议,您可以删除earnings变量并返回getPaymentAmount方法中的值

@Override
public double getPaymentAmount() {
    return hours*wage;
}