员工,名字,姓氏和身份证明

时间:2013-08-27 04:10:49

标签: java

我刚开始并且它真的很混乱,因为当我编写代码时,eclipse上没有红色警告,但是当我运行程序时,它不起作用。 问题是:

  

编写一个程序,显示员工ID以及员工的名字和姓氏。使用两个类。第一个类包含员工数据和用于设置ID和名称的单独方法。另一个类为员工创建对象,并使用对象来调用set方法。创建多个员工并显示他们的数据。

我的代码是:

public class Employee {
    String lastName = null; 
    String firstName = null; 
    double ID; 

    public Employee(String lastName, String firstName, double ID){
        this.lastName = lastName; 
        this.firstName = firstName; 
        this.ID = ID; 
    }

    public String empStat(){
        return "Last Name: " + lastName + "First Name: " + firstName + "ID" + ID; 
    }

}

 public class MainEmployee {
   public static void main(String args[]){

   Employee nub1 = new Employee ("Griffin", "Peter", 000001); 
   System.out.println(nub1);
   Employee nub2 = new Employee ("Griffin", "Lois", 000002); 
   System.out.println(nub2);
   Employee nub3 = new Employee ("Griffin", "Stewie", 000003); 
   System.out.println(nub3); 
   Employee nub4 = new Employee ("Griffin", "Brian", 000004); 
   System.out.println(nub4);


 }
}

它只是显示

Employee@523ce3f
Employee@71b98cbb
Employee@4cc68351
Employee@7cd76237

有人可以告诉我为什么吗?

4 个答案:

答案 0 :(得分:9)

更改

public String empStat()

@Override
public String toString()

docs

中了解toString()如何运作(以及为什么你会看到员工@ 523ce3f

当您使用System.out.println(nub1);时,隐式调用方法nub1.toString()

答案 1 :(得分:0)

必须覆盖toString()方法。只要对象需要返回对象的字符串,就会调用此方法。默认情况下,您会收到“Employee @ 523ce3f”,它是对象的唯一内部表示形式(以字符串形式)。

只需创建一个返回String的方法就不会这样做。

要覆盖toString()方法更改:

public String empStat()

@Override
public String toString()

答案 2 :(得分:0)

您正在尝试打印对象,它将输出对象的哈希码。打印对象在Object类中调用toString()方法,必须覆盖toString方法以获取对象的状态。

public String toString()
{
return firstName+" "+lastName+" "+ ID;
}

覆盖toString是最简单的方法,请考虑<Employee>类型的列表。现在打印列表将返回对象的当前字段值。

List<Employee> list= new ArrayList<Employee>();
Employee o= new Employee("Will","Smith",1);
Employee o1= new Employee("Jason","Bourne",2);
list.add(o);
list.add(o1);
for (Employee x:list)
System.out.print(x);

输出:

[Will Smith 1, Jason Bourne 2]

答案 3 :(得分:0)

您需要toString方法

public String toString() {
    return lastName + " " + firstName + " " + Double.toString(ID);
}

把它放在一起:

class Employee {
    String lastName = null;
    String firstName = null;
    double ID;

    public Employee(String lastName, String firstName, double ID){
        this.lastName = lastName;
        this.firstName = firstName;
        this.ID = ID;
    }

    public String empStat(){
        return "Last Name: " + lastName + "First Name: " + firstName + "ID" + ID;
    }

    public String toString() {
        return lastName + " " + firstName + " " + Double.toString(ID);
    }

}
public class MainEmployee {
    public static void main(String args[]){

        Employee nub1 = new Employee ("Griffin", "Peter", 000001);
        System.out.println(nub1);
        Employee nub2 = new Employee ("Griffin", "Lois", 000002);
        System.out.println(nub2);
        Employee nub3 = new Employee ("Griffin", "ST", 000003);
        System.out.println(nub3);
        Employee nub4 = new Employee ("Griffin", "Brian", 000004);
        System.out.println(nub4);
    }
}

结果如下:

Griffin Peter 1.0
Griffin Lois 2.0
Griffin ST 3.0 
Griffin Brian 4.0