【问题标题】:Mutator methods not storing values for the class that I have just made?Mutator 方法不存储我刚刚创建的类的值?
【发布时间】:2014-07-18 21:00:22
【问题描述】:

这是课程:

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

【问题讨论】:

  • 这很尴尬。学习一个新概念我猜。感谢您的帮助。

标签: java class mutators


【解决方案1】:

你的任务倒退了!

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

【讨论】:

    【解决方案2】:

    您正在将来自Employee 实例的值分配给您传入的参数。为了防止这种情况,使用this 可能是个好主意 -

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多