我必须编写一个Employee Class,然后在Main中调用它来运行测试项目。

我一直在empRate方法上遇到“无法解析符号”的问题。它存在于Employee20类中,但是当我从EmployeeMain20调用该类和方法时,出现“无法解析符号”错误。

网上搜索给我提供了有关IntelliJ错误,更改文件路径和传输项目的各种知识,所有这些都使我感到困惑。 IntelliJ仅安装在此计算机上,尚未从其他来源传输或访问任何文件,并且在其上创建一个干净的副本并不能解决问题。

这是有问题的代码。每行中的第二个“ empRate”是我得到“无法解析符号”的地方。

    Employee20 empRate = new empRate("John", "Smith", 10000, 10, 10);
    Employee20 empRate1 = new empRate("Sue", "Smith", 20000, 10, 10);


这是调用类EmployeeMain20的完整代码:

public class EmployeeMain20 {

    //Year added to differentiate from [other] assignments

    public static void main(String[] args){

        Employee20 empRate = new empRate("John", "Smith", 10000, 10, 10);
        Employee20 empRate1 = new empRate("Sue", "Smith", 20000, 10, 10);

        double wage = empRate.getWage();

        System.out.println("Employee name: " + empRate1.firstName +  empRate1.lastName);
        System.out.println("Employee wage: " + "$" + wage);
        //emp.greet();

        System.out.println("===============================================");

        wage = empRate1.getWage(); //wage defined in Employee2020 class

        System.out.println("Employee name: " + empRate1.firstName +  empRate1.lastName);
        System.out.println("Employee wage: " + "$" + wage);
        //emp.greet();

    }
}


这是被称为Employee20的类的代码:

public class Employee20 {

    //Year added to differentiate from [other] assignments

    String firstName, lastName;
    int baseSalary, overtime, rate;

    public double getWage(){
        double wage = baseSalary * (overtime * rate);
        return wage;
    }

    public void empRate(String fname, String lname, int base, int overTime, int unitrate){
        firstName = fname;
        lastName = lname;
        baseSalary = base;
        overtime = overTime;
        rate = unitrate;
    }


}


我在另一个作业中遇到类似的问题。我认为,如果我能找到解决方案,那么我就能解决。

最佳答案

该类的构造函数必须与该类的名称相同,并且没有返回类型

public Employee20 (String fname, String lname, int base, int overTime, int unitrate){
    firstName = fname;
    lastName = lname;
    baseSalary = base;
    overtime = overTime;
    rate = unitrate;
}


当您调用它时,您可以按照

Employee20 empRate = new Employee20("John", "Smith", 10000, 10, 10);

10-07 22:37