int不能被解除引用?

时间:2012-12-06 14:07:32

标签: java super jcreator

编译完程序后,我收到了显示给我的错误消息

 Resource: Employee.java
 int cannot be dereferenced

代码

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

经过一段时间的检查后,我仍然无法修复错误。 这是我的课程,

员工

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;

    }

}

薪酬

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 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;
    }

}

学术

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;
    }
}

2 个答案:

答案 0 :(得分:7)

这个是问题,而不是构造函数:

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

考虑到salary.setMonthlySalarysalary参数,请查看int - 您期望该做什么?

我怀疑你打算打电话给this.salaray.setMonthlySalary(salary)

顺便说一下,这不是你遇到的唯一问题。只是看一下你的代码,我怀疑这不符合你的要求:

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

您真的需要了解代码何时引用参数,以及何时引用字段。如果你在两者之间感到困惑,请使用不同的名称。

此外:

  • 为什么您只允许使用int构建薪水,然后将其存储为double
  • 您不应该首先使用double作为货币值。要么使用int隐式缩放(例如分数),要么使用BigDecimal

答案 1 :(得分:3)

正如错误消息所示

Resource: Employee.java
 int cannot be dereferenced

这意味着您无法取消引用int中的Employee值。它没有提及薪水。

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

您有一个名为salary的参数,它是int这会隐藏具有相同名称的字段。要解决此问题,您可以

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

或更好的是使名称不同,因为它们有不同的目的。

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

我建议你查看IDE中的代码,因为它可以帮助你找到错误的来源并帮助你解决它们。

相关问题