2013-08-27 27 views
3

我刚开始,这真的让人困惑,因为当我编写代码时,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); 


} 
} 

和所有它的作用是显示

[email protected] 
[email protected] 
[email protected] 
[email protected]

有人可以告诉我为什么?

+0

请注意:[JavaScript不是JAVA](http://kb.mozillazine.org/JavaScript_is_not_Java) – mplungjan

+0

不要养成零填充数字的习惯。 '000010'不是你认为的数字。另见http://stackoverflow.com/questions/5206342/what-is-an-illegal-octal-digit/5206381#5206381 –

回答

9

变化

public String empStat() 

@Override 
public String toString() 

查看如何toString()作品(为什么你看到员工@ 523ce3f)在docs

当您使用System.out.println(nub1);方法nub1.toString()被称为含蓄。

0

必须重写toString()方法。这被称为任何时候对象需要返回一个对象的字符串。默认情况下,您会收到'Employee @ 523ce3f',它是对象的唯一内部表示形式(以字符串形式)。

简单地创建一个返回String的方法不会这样做。

要覆盖toString()方法的变化:

public String empStat()

 
@Override 
public String toString() 
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] 
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