我必须编写一个Employee类,然后在Main中调用它来运行测试项目。
我在方法 empRate 上不断收到“无法解析符号”的消息。它存在于 Employee20 类中,但是当我从 EmployeeMain20 调用该类和方法时,出现“无法解析符号”错误。
网络搜索为我提供了各种有关 IntelliJ bug、更改文件路径和传输项目的信息,所有这些都让我感到困惑。 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);
关于java - 员工类别: cannot resolve symbol in EmployeeMain,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59728542/