运行代码时会产生此错误
Employee cannot be applied to given types;
required: no arguments
found: java.lang.String,double,int
reason: actual and formal argument lists differ in length
主类:
public class Salary {
public static void main(String[] args) {
Employee worker = new Employee("harry",5.5,45);
System.out.println(worker.getEmployeeName());
System.out.println("Your Gross Salary is"+ worker.getGrossSalary());
System.out.println("after tax is"+ worker.getNetSalary());
}
}
Employee类的构造函数:
public void Employee(String n , double pr, int h){
name = n;
payRate = pr;
hours = h;
}
最佳答案
构造函数不能也不应该有返回类型。
public void Employee(String n, double pr, int h){ // this is considered a method that belongs to that particular object
name = n;
payRate = pr;
hours = h;
}
更改为:
public Employee(String n , double pr, int h){ // this is considered a constructor, invoked when you're making an object of this type
name = n;
payRate = pr;
hours = h;
}
关于Java-错误 : Actual and formal argument lists differ in length,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42462692/