Хорошо, так что это достаточно просто.
Мне нужно распечатать все параметры объекта по определенному индексу. Однако все объекты отображают адрес памяти. Я знаю, что вы должны переопределить метод 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
}//конец класса
и вот код в моем основном методе:
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
Поместите toString()
в класс Employee, возможно, с тегом @Override над ним.
@Override
public String toString() {
return name + ", " + age + ", " + department;
}
class Employee {
@Override
public String toString() {
return String.format("[Employee name=%s, age=%d, department=%s]",
name, age, department);
}
}
Теперь просто перейдем к массиву сотрудников и просто напечатайте объект employee, как вы уже знаете.