-1

编译程序后,我收到这条错误消息,显示我

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

2 回答 2

7

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

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

看看salary.setMonthlySalary- 你期望它做什么,因为这salary是一个int参数?

我怀疑你是打算打电话this.salaray.setMonthlySalary(salary)的。

顺便说一句,这不是你遇到的唯一问题。看看你的代码,我怀疑这不能满足你的要求:

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

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

此外:

  • 为什么您只允许使用 a 构造薪水int,然后将其存储为 a double
  • 首先,您不应该使用double货币价值。要么使用int带有隐式缩放的 a (例如美分数),要么使用 a BigDecimal
于 2012-12-06T14:09:12.317 回答
3

正如错误消息所说

Resource: Employee.java
 int cannot be dereferenced

这意味着您不能取消引用它没有提到薪水中的int值。Employee

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

您有一个称为参数的参数salaryint 它隐藏了具有相同名称的字段。要解决此问题,您可以这样做

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

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

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

我建议您查看 IDE 中的代码,因为它将帮助您找到错误的来源并帮助您修复它们。

于 2012-12-06T14:12:43.563 回答