2014-07-18 77 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会给你一个编译时错误。

相关问题