Java-错误 : Actual and formal argument lists differ in length

标签 java

运行代码时会产生此错误

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/

相关文章:

java - 是否可以拦截对类方法的任何调用?

java - 像在纸板应用程序中一样平滑传感器数据

java - 通过EventChannel向Flutter发送Android短信BroadcastReceiver数据

java - 遍历可变长度数组

java - 如何修改struts中的html...链接到jsp

java - Circle 类多个构造函数

java - SoapUI。从源代码构建失败并显示 "unresolved dependencies"

java - 将子字符串与 Java 中的字符串进行比较

java - 更改 arraylist 内的 class 内的整数值?

java - 半透明 JLabel 未正确显示背景