2014-07-18 106 views
-1

這裏是類:Mutator方法不存儲我剛剛創建的類的值?

package employee; 

public class Employee 

{ 
    private String name, department,position; 
    private int idNumber; 

    public Employee(String n, int id, String depart,String pos) 
      { 
       n= name; 
       id=idNumber; 
       depart=department; 
       pos=position; 

      } 
    public Employee(String n, int id) 
      { 
       n= name; 
       id=idNumber; 
       department=""; 
       position=""; 
      }  
    public Employee() 
      { 
       name=""; 
       idNumber=0; 
       department=""; 
       position="";     
      } 
    public void setName(String n) 
    { 
     n=name; 

    } 
    public void setDepartment(String depart) 
    { 
     depart=department; 
    } 
    public void setPosition(String pos) 
    { 
     pos=position; 
    } 
    public void setId(int id) 
    { 
     id=idNumber; 
    } 
    public String getName() 
    { 
     System.out.println(); 
     return name; 
    } 
    public String getDepartment() 
    { 
     return department; 
    } 
    public String getPosition() 
    { 
     return position; 
    } 
    public int getId() 
    { 
     return idNumber; 
    } 
} 

下面是程序:

package employee; 

public class RunEmployee 
{ 

    public static void main(String[] args) 
    { 
     Employee first= new Employee(); 

     first.setName("Susan Myers"); 
     first.setId(47899); 
     first.setDepartment("Accounting"); 
     first.setPosition("Vice President"); 

     Employee sec= new Employee("Mark Jones",39119,"IT","Programmer"); 

     Employee third= new Employee("Joy Rogers", 81774); 
     third.setDepartment("Manfacturing"); 
     third.setPosition("Engineer"); 

     /*Printing employee ones information*/ 

     System.out.print("Employee #1- using the no-arg constructor."); 
     System.out.println("Name: " + first.getName()); 
     System.out.println("Id Number: "+ first.getId()); 
     System.out.println("Department: " + first.getDepartment()); 
     System.out.println("Position: "+ first.getPosition()); 

     /*Printing employee twos information*/ 

     System.out.println("Name: " + sec.getName()); 
     System.out.println("Id Number: "+ sec.getId()); 
     System.out.println("Department: " + sec.getDepartment()); 
     System.out.println("Position: "+ sec.getPosition()); 

     /*Printing employee threes information*/ 
     System.out.print("Employee #3- using a constructor that accepts the name" 
       + "and ID number only."); 
     System.out.println("Name: " + third.getName()); 
     System.out.println("Id Number: "+ third.getId()); 
     System.out.println("Department:" + third.getDepartment()); 
     System.out.println("Position: "+ third.getPosition()); 






    } 

} 

對於這個項目,我只是想值存儲到不同的方式構造。但是,我的輸出顯示我的mutator方法沒有存儲任何值。我試圖發佈我的輸出,但我沒有聲望點。基本上,我嘗試參數的所有值都是零或爲空。

+0

這是令人尷尬的。我猜想,學習一個新概念讓我感到困惑。謝謝您的幫助。 – Chevyb

回答

2

你已經完成了作業!

n = name;name的值設置爲n,而不是相反。

2

您正在將Employee實例中的值賦值給傳入的參數。爲了防止這種情況,很可能是使用this一個好主意 -

this.name = n; // <-- assign n to the name field of the current instance. 

在你的示例代碼,this.n會給你一個編譯時錯誤。

相關問題