2013-10-01 51 views
0

对于此问题,我得到了以下层次结构:基类Person,子类Employee和子类Staff和Supervisor。我需要在Employee,Staff和Supervisor类中应用方法重写。驾驶员课程的主要方法需要创建员工和主管的多个对象,并显示他们的姓名和月薪。我建立了一个多态的数组在驱动类,但我想不出如何构造for循环显示重写的方法返回值旁边的数组的每个元素。我不停的基础和子类非常简单:在Java中打印多态数组的不同元素

public class Person { 


void Salary(){ 
System.out.println("All positions are salaried"); 

} 
} 
public class Employee extends Person { 

void Salary(){ 
    System.out.println("Salaries vary between staff and supervisors"); 
} 

} 
public class Staff extends Employee { 

public Staff(String string) { 

} 

void Salary() { 
    System.out.println("A regular staff employee makes 3500.00 monthly"); 
} 


} 
public class Supervisor extends Employee { 


public Supervisor(String string) { 
} 

void Salary(){ 
    System.out.println("Supervisors make approximately 9800.00 monthly"); 
} 
} 

这里是驱动程序类,在那里我遇到的问题:眼下

import java.util.Arrays; 

public class HRRoster { 


public static void main(String[] args) { 


    Person roster[] = new Person[6]; 
    roster[0] = new Supervisor("James Martin"); 
    roster[1] = new Supervisor("William Smith"); 
    roster[2] = new Supervisor("Jennifer Shipman"); 
    roster[3] = new Staff("Brian Williams"); 
    roster[4] = new Staff("Carrie James"); 
    roster[5] = new Staff("Samantha Powers"); 



      for (int i=0; i < roster.length; i++){ 
       System.out.print(Arrays.toString(roster)); 
       roster[i].Salary(); 
      } 
    } 

} 

,它的输出是所有的元素数组加上重写的方法,每个都在它自己的行上。我知道我在我的for循环中错过了一些荒谬的初级/简单的东西,但我不确定如何从这里开始工作。基本上我想列出数组中的每个元素,然后执行重写的方法,每个元素都在自己的行上。

+0

您可能需要使用'println'新线路输出。 –

回答

0

你期待下面的行为

import java.util.Arrays; 

class Person { 
    String name; 
    void Salary() { 
     System.out.println("All positions are salaried"); 

    } 
    public String toString(){ 
     return name; 
    } 
} 

class Employee extends Person { 

    void Salary() { 
     System.out.println("Salaries vary between staff and supervisors"); 
    } 
    public String toString(){ 
     return name; 
    } 

} 

class Staff extends Employee { 

    public Staff(String name) { 
     this.name = name; 
    } 

    void Salary() { 
     System.out.println("A regular staff employee makes 3500.00 monthly"); 
    } 
    public String toString(){ 
     return name; 
    } 


} 

class Supervisor extends Employee { 

    public Supervisor(String name) { 
     this.name = name; 
    } 

    void Salary() { 
     System.out.println("Supervisors make approximately 9800.00 monthly"); 
    } 
    public String toString(){ 
     return name; 
    } 
} 

public class HRRoster { 

    public static void main(String[] args) { 

     Person roster[] = new Person[6]; 
     roster[0] = new Supervisor("James Martin"); 
     roster[1] = new Supervisor("William Smith"); 
     roster[2] = new Supervisor("Jennifer Shipman"); 
     roster[3] = new Staff("Brian Williams"); 
     roster[4] = new Staff("Carrie James"); 
     roster[5] = new Staff("Samantha Powers"); 

     for (int i = 0; i < roster.length; i++) { 
      System.out.print(roster[i].toString() + "\t"); 
      roster[i].Salary(); 
     } 
    } 

} 
+0

非常接近;我正在寻找的东西,以实际显示他们的名字/元素的名单旁边,他们做了多少...例如:詹姆斯马丁监督约9800.00每月。 – DavesNotHere

+0

更新了答案,我强烈建议你通过基础知识。并接受答案,如果它帮助你 – upog

+0

杜。我知道我错过了一些简单的事情。也知道我还有点生疏;经过一年的休息和一个蹩脚的老师刚刚回到这一点。 – DavesNotHere