2012-12-06 3 views
-1

내가 여전히 오류가 해결되지 수있는 몇 시간 후 확인한 후이 코드int를 참조 해제 할 수 없습니까?

public Salary(int positionSalary) { 
     setMonthlySalary(positionSalary); 
    } 

나에게

Resource: Employee.java 
int cannot be dereferenced 

을 보여줍니다이 오류 메시지를 가지고 프로그램을 컴파일 후. 이 내 수업,

직원

import java.util.*; 
import java.lang.String.*; 
public class Employee { 
    protected int type; 
    private static int staticID = 0001; 
    protected int id; 
    protected String name; 
    protected String ic; 
    protected String tel; 
    protected static int count = 0; 
    protected int editChoice=0; 
    private Salary salary = new Salary(0); 

    public Employee(){ 
     id = -1; 
     name = ""; 
     ic = ""; 
     tel = ""; 
    } 

    public Employee(int type, String name, String ic, String tel) { 
     this.type = type; 
     this.id = staticID++; 
     this.name = name; 
     this.ic = ic; 
     this.tel = tel; 
     this.salary = salary; 
     count++; 
    } 

    public void setId(int id){ 
     this.id = id; 
    } 

    public int getId(){ 
     return id; 
    } 

    public void setName(String name){ 
     this.name = name; 
    } 

    public String getName(){ 
     return name; 
    } 

    public void setIc(String ic){ 
     this.ic = ic; 
    } 

    public String getIc(){ 
     return ic; 
    } 

    public void setTel(String tel){ 
     this.tel = tel; 
    } 

    public String getTel(){ 
     return tel; 
    } 

    public void setSalary(int salary){ 
     salary.setMonthlySalary(salary); 
    } 

    public double getSalary(){ 
     return salary.getMonthlySalary(); 
    } 

    public static void setCount(int count){ 
     count =count ; 
    } 

    public static int getCount(){ 
     return count; 
    } 

    public static void addCount(){ 
     count++; 
    } 

    public String toString(){ 
     return getId()+"/t/t" +getName()+ "\t\t" +getIc()+ "\t\t" +getTel(); 
    } 

    //Add Employee 
    public void addEmp(){ 
     Scanner add = new Scanner(System.in); 
     System.out.print("Enter New Employee Name :"); 
     String newName = add.nextLine(); 
     System.out.print("Enter New Employee IC :"); 
     String newIC = add.nextLine(); 
     System.out.print("Enter New Employee Phone Number :"); 
     String newPhoneNumber = add.nextLine(); 

     this.id = staticID++; 
     setName(newName.toUpperCase()); 
     setIc(newIC); 
     setTel(newPhoneNumber); 
     this.id=staticID++; 

    } 

    //Delete Employee 
    public void deleteEmp(){ 
     this.id = 0; 
     System.out.println("Deletion Successfull."); 
    } 

    //Edit Employee Details 
    public void editEmp(){ 
     Scanner edit = new Scanner(System.in); 
     int choice= 0; 
     choice=editMenu(); 
     switch(choice){ 
      case 1: 
       System.out.print("Enter Employee Name: "); 
       String newName = edit.nextLine(); 
       setName(newName.toUpperCase()); 
       break; 
      case 2: 
       System.out.print("Enter Employee IC: "); 
       String newIC = edit.nextLine(); 
       setIc(newIC); 
       break; 
      case 3: 
       System.out.print("Enter Employee Contact Number: "); 
       String newTel = edit.nextLine(); 
       setTel(newTel); 
       break; 
      case 4: 
       this.editChoice=4; 
       break; 
     } 
    } 
    public int getEditChoice(){ 
     return this.editChoice; 
    } 
    public void setEditChoice(int editChoice){ 
     this.editChoice=editChoice; 
    } 

    public int editMenu(){ 
     Scanner input = new Scanner(System.in); 
     System.out.println("You Are Currently Modifying:\n"+ 
          "\nID:"+getId()+" Name:"+getName()+ 
          "\n======================================"+ 
          "\n\n1.Employee Name\n" + 
          "2.Employee IC\n" + 
          "3.Employee Contact Number\n" + 
          "4.Continue\n"); 
     int choice = input.nextInt(); 

     return choice; 

    } 

} 

급여

import java.util.*; 
import java.lang.String.*; 
public class Admin extends Employee{ 
    private int position;// head of school,head of division,assistant ,senior , manager 
    private String positionS; 
    private String department;// accounting,finance ,operational ,marketing 
    private int workingYears; 
    private static int adminCount = 0; 

    public Admin() { 
     super(); 
    } 

    public Admin(String name, String ic, String tel, int position, String department, int workingYears) { 
     super(1, name, ic, tel); 
     this.position = position; 
     this.department = department; 
     this.workingYears = workingYears; 
     adminCount++; 

    } 

    public String getPosition(){ 
     positionToString(this.position); 
     return this.positionS; 
    } 
    public void setPosition(int position){ 
     this.position = position; 
    } 
    public void positionToString(int position){ 
     switch(position){ 
      case 1: 
       this.positionS="HEAD OF SCHOOL"; 
       break; 
      case 2: 
       this.positionS="HEAD OF DIVISON"; 
       break; 
      case 3: 
       this.positionS="ASSISTANT"; 
       break; 
      case 4: 
       this.positionS="SENIOR"; 
       break; 
      case 5: 
       this.positionS="MANAGER"; 
       break; 
     } 
    } 

    public void setDepartment(String department){ 
     this.department = department; 
    } 

    public String getDepartment(){ 
     return department; 
    } 

    public void setWorkingYears(int workingYears){ 
     this.workingYears = workingYears; 
    } 

    public int getWorkingYears(){ 
     return workingYears; 
    } 

    public double calculateSalary(){ 
     if(workingYears>5){ 
      return super.getSalary()+2000; 
     } 
     else 
      return super.getSalary()+1000; 
    } 

    public static void setAdminCount(int adminCount){ 
     adminCount=adminCount; 
    } 

    public static int getAdminCount(){ 
     return adminCount; 
    } 

    public String toString(){ 
     return super.toString()+ "\t\t" +getPosition()+ "\t\t" +getDepartment()+ "\t\t\t" +getWorkingYears()+ "\t\t" +calculateSalary(); 
    } 

    //Add Admin Employee 
    public void addEmp(){ 
     super.addEmp(); 
     Scanner add = new Scanner(System.in); 
     System.out.print("1.Head of School\n"+ 
         "2.Head of Division\n"+ 
         "3.Assistant\n"+ 
         "4.Senior\n"+ 
         "5.Manager\n\n"+ 
         "Please Enter Your Choice:"); 
     int newPositionS = add.nextInt(); 
     int newPosition = newPositionS; 
     setPosition(newPosition); 
     System.out.print("Enter Employee Department: "); 
     String newDepartment = add.nextLine(); 
     setDepartment(newDepartment); 
     this.adminCount++; 
    } 

    //Search Admin Employee 
    public void searchAdminEmp(){ 
     System.out.print("ADMINISTRATIVE EMPLOYEE"+ 
         "\n================================"+ 
         "\nID: "+super.getId()+ 
         "\nName: "+super.getName()+ 
         "\nNRIC: "+super.getIc()+ 
         "\nTel No.: "+super.getTel()+ 
         "\nPosition: "+getPosition()+ 
         "\nSalary: "+getSalary() + 
         "\nDepartment: "+getDepartment()); 
    } 

    //Edit Admin Employee Detail 
    public void editEmp(){ 
     super.editEmp(); 
     Scanner edit = new Scanner(System.in); 
     int check = super.getEditChoice(); 
     if(check==5){ 
      switch(EditAdminMenu()) { 
       case 6: 
        System.out.print("1.Head of School\n"+ 
            "2.Head of Division\n"+ 
            "3.Assistant\n"+ 
            "4.Senior\n"+ 
            "5.Manager\n\n"+ 
            "Please Enter Your Choice:"); 
        int newPositionS = edit.nextInt(); 
        int newPosition = newPositionS; 
        super.setSalary(newPosition); 
        setPosition(newPosition); 
        break; 
       case 7: 
        System.out.print("Enter Employee Department: "); 
        String newDepartment = edit.nextLine(); 
        setDepartment(newDepartment); 
        break; 
       case 0: 
        super.setEditChoice(0); 
        break; 
       default: 
        System.out.print("Wrong Input, please try again."); 
        break; 
      } 
     } 
    } 
    public int EditAdminMenu(){ 
     Scanner edit = new Scanner(System.in); 
     System.out.print("=======Administrative==========\n"+ 
          "6.Employee Position\n"+ 
          "7.Employee School\n"+ 
          "0.Back\n\n"); 
     int choice = edit.nextInt(); 

     return choice; 
    } 

} 

학술

public class Salary { 
    private double monthlySalary; 

    public Salary(){ 
    } 

    public Salary(int positionSalary) { 
     setMonthlySalary(positionSalary); 
    } 

    public double getMonthlySalary(){ 
     return this.monthlySalary; 
    } 
    public void setMonthlySalary(int positionSalary){ 
     switch(positionSalary){ 
      case 0: 
       this.monthlySalary=0.00; 
       break; 
      case 1: 
       this.monthlySalary=4500.00; 
       break; 
      case 2: 
       this.monthlySalary=4000.00; 
       break; 
      case 3: 
       this.monthlySalary=3500.00; 
       break; 
      case 4: 
       this.monthlySalary=3000.00; 
       break; 
      case 5: 
       this.monthlySalary=2500.00; 
       break; 
      case 6: 
       this.monthlySalary=2000.00; 
       break; 
      case 7: 
       this.monthlySalary=1500.00; 
       break; 
     } 
    } 

} 

관리자입니다

import java.util.*; 
import java.lang.String.*; 
public class Academic extends Employee{ 
    private String school;//SAS, SBS, SSSH , SPUS , SOT 
    private int position;// lecturer ,tutor 
    private String positionS; 
    private int workingYears; 
    private static int acaCount = 0; 

    public Academic(){ 
     super(); 
    } 

    public Academic(String name, String ic, String tel, String school, int position, int workingYears){ 
     super(2, name, ic, tel); 
     this.school = school; 
     this.position = position; 
     this.workingYears = workingYears; 
     acaCount++; 

    } 

    public void setSchool(String school){ 
     this.school = school; 
    } 

    public String getSchool(){ 
     return school; 
    } 

    public void setPosition(int position){ 
     this.position = position; 
    } 

    public String getPosition(){ 
     positionToString(this.position); 
     return this.positionS; 
    } 

    public void positionToString(int position){ 
     switch(position){ 
      case 6: 
       this.positionS="LECTURER"; 
       break; 
      case 7: 
       this.positionS="TUTOR"; 
       break; 
      default: 
       break; 
     } 
    } 

    public void setWorkingYears(int workingYears){ 
     this.workingYears = workingYears; 
    } 

    public int getWorkingYears(){ 
     return workingYears; 
    } 

    public double calculateSalary(){ 
     if(workingYears>5){ 
      return super.getSalary()+1000; 
     } 
     else 
      return super.getSalary()+500; 
    } 

    public static void setAcaCount(int acaCount){ 
     acaCount=acaCount; 
    } 

    public static int getAcaCount(){ 
     return acaCount; 
    } 



    public String toString(){ 
     return super.toString()+ "\t" +getSchool()+ "\t\t" +getPosition()+ "\t\t" +getWorkingYears()+"\t\t\t" +calculateSalary() ; 
    } 

    //Add Admin Employee 
    public void addEmp(){ 
     super.addEmp(); 
     Scanner add = new Scanner(System.in); 
     System.out.print("Enter Employee School: "); 
     String newSchool = add.nextLine(); 
     setSchool(newSchool.toUpperCase()); 
     System.out.print("6.Tutor\n"+ 
         "7.Lecturer\n\n"+ 
         "Please Enter Your Choice:"); 
     int newPositionS = add.nextInt(); 
     int newPosition = newPositionS; 
     setPosition(newPosition); 
     this.acaCount++; 
    } 

    //Search Admin Employee 
    public void searchAcaEmp(){ 
     System.out.print("ADMINISTRATIVE EMPLOYEE"+ 
         "\n================================"+ 
         "\nID: "+super.getId()+ 
         "\nName: "+super.getName()+ 
         "\nNRIC: "+super.getIc()+ 
         "\nTel No.: "+super.getTel()+ 
         "\nSchool: "+getSchool()+ 
         "\nPosition: "+getPosition()+ 
         "\nSalary: "+getSalary()); 
    } 

    //Edit Admin Employee Detail 
    public void editEmp(){ 
     super.editEmp(); 
     Scanner edit = new Scanner(System.in); 
     int check = super.getEditChoice(); 
     if(check==5){ 
      switch(EditAcaMenu()) { 
       case 6: 
        System.out.print("Enter Employee School: "); 
        String newSchool = edit.nextLine(); 
        setSchool(newSchool.toUpperCase()); 
        break; 
       case 7: 
        System.out.print("6.Tutor\n"+ 
            "7.Lecturer\n\n"+ 
            "Please Enter Your Choice:"); 
        int newPositionS = edit.nextInt(); 
        int newPosition = newPositionS; 
        super.setSalary(newPosition); 
        setPosition(newPosition); 
        break; 
       case 0: 
        super.setEditChoice(0); 
        break; 
       default: 
        System.out.print("Wrong Input, please try again."); 
        break; 
      } 
     } 
    } 
    public int EditAcaMenu(){ 
     Scanner edit = new Scanner(System.in); 
     System.out.print("================Academic================\n"+ 
          "6.Employee Position\n"+ 
          "7.Employee School\n"+ 
          "0.Back\n\n"); 
     int choice = edit.nextInt(); 

     return choice; 
    } 
} 
,691 당신이 salaryint 매개 변수가 주어진 할을 기대 않습니다 -

public void setSalary(int salary){ 
    salary.setMonthlySalary(salary); 
} 

salary.setMonthlySalary에서 봐 :

+0

import java.lang.String. *?!? 나는 그것이 실제로 불필요하다는 사실 외에도 실제로 컴파일 된 것에 놀랐다. – Robin

답변

7

하지 생성자 문제인가?

this.salaray.setMonthlySalary(salary)에 전화하기로 한 것 같습니다.

그런데 문제는 그게 아닙니다. 그냥 코드를이기는, 나는 이것이 당신이 원하는 것을하지 않습니다 의심 :이 필드를 참조 때

public static void setCount(int count){ 
    count =count ; 
} 

당신은 정말 당신의 코드는 매개 변수를 참조하면 이해할 필요합니다. 이 둘 사이에 혼란 스러울 경우 다른 이름을 사용하십시오. 또한

:

  • 왜 만 int으로 급여가 구축 될 수 있도록되어 있지만, 다음 double로 저장?
  • 처음에는 통화 값에 double을 사용하지 않아야합니다. 암시 적 크기 조정 (예 : 센트 수)을 사용하여 int을 사용하거나 BigDecimal을 사용하십시오. 오류 메시지로
3

은 당신이 Employeeint 값 역 참조 그것은 급여를 언급하지 않고 할 수 없음을 의미

Resource: Employee.java 
int cannot be dereferenced 

말한다.

public void setSalary(int salary){ 
    salary.setMonthlySalary(salary); 
} 

당신이이 같은 이름의 필드를 숨기는 int입니다 salary라는 매개 변수가 있습니다.이 문제를 해결하려면

public void setSalary(int salary){ 
    this.salary.setMonthlySalary(salary); 
} 

이상은 다른 목적으로 사용되는 이름을 다른 것으로 만드는 것입니다.

public void setSalary(int monthlySalary){ 
    salary.setMonthlySalary(monthlySalary); 
} 

오류가 발생한 위치를 찾고 문제를 해결하는 데 도움이되므로 IDE의 코드를 살펴 보시기 바랍니다.

관련 문제