Mutator方法没有存储我刚刚制作的类的值?

时间:2014-07-18 21:00:22

标签: java class mutators

这是班级:

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方法没有存储任何值。我试图发布我的输出但我没有声望点。基本上,我尝试参数的东西的所有值都表示零或空。

2 个答案:

答案 0 :(得分:2)

你的作业倒退了!

n = name;name的值设为n,而不是相反。

答案 1 :(得分:2)

您正在将Employee实例中的值分配给传入的参数。为了防止这种情况,使用this -

可能是个好主意
this.name = n; // <-- assign n to the name field of the current instance.

在您的示例代码中,this.n会给您一个编译时错误。