2013-08-27 41 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