带有Java数组的instanceof

带有Java数组的instanceof,java,instanceof,Java,Instanceof,我正在使用一个名为Employee的类和两个名为SalariedEmployee和HourlyEmployee的子类。在我的代码的这一部分中,我试图测试数组中的员工是受薪员工还是小时员工。但是,它只是从原始Employee类打印属性,而不是从子类打印属性 case 2: { for(int index = 0; index < employees.length; index++ ) { System.out.println( employees[index]

我正在使用一个名为Employee的类和两个名为SalariedEmployee和HourlyEmployee的子类。在我的代码的这一部分中,我试图测试数组中的员工是受薪员工还是小时员工。但是,它只是从原始Employee类打印属性,而不是从子类打印属性

case 2: {
     for(int index = 0; index < employees.length; index++ ) {
          System.out.println( employees[index] + "\n" );
          if(employees[index] instanceof SalariedEmployee) {
              SalariedEmployee aSalariedEmployee = (SalariedEmployee) employees[index];
              System.out.println( aSalariedEmployee.toString() );
          }
          else if(employees[index] instanceof HourlyEmployee) {
               HourlyEmployee anHourlyEmployee = (HourlyEmployee) employees[index];
               System.out.println( anHourlyEmployee.toString() );
          }
          else {
               System.out.println( " " );
          }
      }
      System.out.println( " " );
      break;
 }
我觉得这里缺少的是我需要以某种方式将typeEmployee与对象本身关联起来。有人知道我怎么做吗? 谢谢你的帮助

它只打印原始Employee类的属性, 不是来自子类

case 2: {
     for(int index = 0; index < employees.length; index++ ) {
          System.out.println( employees[index] + "\n" );
          if(employees[index] instanceof SalariedEmployee) {
              SalariedEmployee aSalariedEmployee = (SalariedEmployee) employees[index];
              System.out.println( aSalariedEmployee.toString() );
          }
          else if(employees[index] instanceof HourlyEmployee) {
               HourlyEmployee anHourlyEmployee = (HourlyEmployee) employees[index];
               System.out.println( anHourlyEmployee.toString() );
          }
          else {
               System.out.println( " " );
          }
      }
      System.out.println( " " );
      break;
 }
您需要重写子类中的
toString()
,以包含新属性

它只打印原始Employee类的属性, 不是来自子类

case 2: {
     for(int index = 0; index < employees.length; index++ ) {
          System.out.println( employees[index] + "\n" );
          if(employees[index] instanceof SalariedEmployee) {
              SalariedEmployee aSalariedEmployee = (SalariedEmployee) employees[index];
              System.out.println( aSalariedEmployee.toString() );
          }
          else if(employees[index] instanceof HourlyEmployee) {
               HourlyEmployee anHourlyEmployee = (HourlyEmployee) employees[index];
               System.out.println( anHourlyEmployee.toString() );
          }
          else {
               System.out.println( " " );
          }
      }
      System.out.println( " " );
      break;
 }

您需要重写子类中的
toString()
,以包含新属性。

不应该
while(loop=true)
while(loop)
(没有可能有害的赋值)或
for(;;)
(简单无限循环)?是,这对我来说是一个粗心的错误,但它仍然不能回答更大的问题。您不需要检查受薪员工的
实例
员工[index]。toString()
((受薪员工)员工[index])。toString()
调用完全相同的方法。@Andy Turner您建议我做什么来代替它?while(loop=true)
不应该
while(loop)
(没有可能有害的赋值)或
for(;;)
(简单无限循环)?是,这对我来说是一个粗心的错误,但它仍然不能回答更大的问题。您不需要检查受薪员工的
实例
员工[index]。toString()
((受薪员工)员工[index])。toString()
调用完全相同的方法。@AndyTurner您建议我做什么来代替它?如果这就是您所需要的,您根本不需要执行
实例和强制转换。如果employee实例知道如何打印自己,则调用方法不需要区分。多态性之美。@Thilo我已经在子类中的toString方法之前添加了覆盖,但它仍然不起作用。请在问题中包括所有
toString
方法的实现。如果这就是您所需要的,您根本不需要执行
instanceof
和强制转换。如果employee实例知道如何打印自己,则调用方法不需要区分。多态性之美。@Thilo我已经在子类中的toString方法之前添加了覆盖,它仍然不起作用。请在问题中包括所有
toString
方法的实现。