如何打印出具有参数的对象数组?

MrT*_*eos 1 java

好吧,这很简单.

我需要在特定索引处打印出对象的所有参数.但是,所有对象都显示内存地址.我知道你必须覆盖toString方法.但我不太确定在哪里.

任何帮助都会很棒!

这是我的Employee类代码:

public class Employee {

private String name;
private int age;
private String department;

public String getDept(){
    return department;
}//end dept

public void setDept(String dept){
    this.department = dept;
}//end

public String getName(){
    return name;
}//end name

public void setName(String n){
    this.name = n;
}//end

public int getAge(){
    return age;
}//end age

public void setAge(int a){
    this.age = a;
}//end

public Employee (String n,int age,String dept){

    this.name = n;
    this.age = age;
    this.department = dept;

}//end employee
Run Code Online (Sandbox Code Playgroud)

} //结束类

这是我的main方法中的代码:

public class Company {


public static void main(String [] args){

    Employee[] e = new Employee[13];

    e[0] = new Employee("Counting Guru",55,"Accounting");
    e[1] = new Employee("Counting Pro",45,"Accounting");
    e[2] = new Employee("Counting Savvy",40,"Accounting");
    e[3] = new Employee("Counting Novice",25,"Accounting");
    e[4] = new Employee("Sales Guru",50,"Marketing");
    e[5] = new Employee("Sales Pro",48,"Marketing");
    e[6] = new Employee("Sales Savvy",38,"Marketing");
    e[7] = new Employee("Hiring Guru",58,"Human Resrouces");
    e[8] = new Employee("Hiring Pro",47,"Human Resrouces");
    e[9] = new Employee("Hacking Pro",47,"Information Systems");
    e[10] = new Employee("Hacking Guru",51,"Information Systems");
    e[11] = new Employee("Hacking Savvy",38,"Information Systems");
    e[12] = new Employee("Hacking Novice",23,"Information Systems");


    for(int i = 0;i<e.length;i++){
        System.out.println(e[i]);

    }//end 
}//end main
}//end company
Run Code Online (Sandbox Code Playgroud)

La-*_*eja 5

放在toString()Employee类中,可能上面有@Override标记.

@Override
public String toString() {
    return name + ", " + age + ", " + department;
}
Run Code Online (Sandbox Code Playgroud)