2014-07-18 4 views
-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 메서드가 어떤 값도 저장하지 않는다는 것을 보여줍니다. 출력을 올리려고했지만 평판 포인트가 없습니다. 기본적으로 논쟁을 시도한 모든 값은 0 또는 null이라고 말합니다.

+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 당신에게 컴파일시 오류를 준 것이다.

관련 문제