-1

这是课程:

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 方法没有存储任何值。我试图发布我的输出,但我没有声誉积分。基本上,我尝试参数的所有值都说为零或空。

4

2 回答 2

2

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

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

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

于 2014-07-18T21:08:13.250 回答
2

你的任务倒退了!

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

于 2014-07-18T21:01:25.713 回答