Java 错误 - 实际和形式参数列表的长度不同

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22813484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 18:05:28  来源:igfitidea点击:

Java Error - Actual and formal argument lists differ in length

java

提问by Dee

I am trying to call a method, but it is giving this error:

我正在尝试调用一个方法,但它给出了这个错误:

java:112: error:required:String, String

found:String

reason:actual and formal arguments lists differ in length

java:112: 错误:必需:字符串, 字符串

找到:字符串

原因:实际和形式参数列表的长度不同

Here is the method I'm trying to call:

这是我试图调用的方法:

public void setShippingDest(String inCustName, String inDestn) {
    // ...
}

Here is how I'm trying to call it:

这是我试图称呼它的方式:

shipOrder.setShippingDest("Broome");

采纳答案by Jon Skeet

Well it's quite simple. Here's the declaration of setShippingDest:

嗯,这很简单。以下是声明setShippingDest

public void setShippingDest(String inCustName, String inDestn)

And here's how you're trying to call it:

这是您尝试调用它的方式:

shipOrder.setShippingDest("Broome");

You've provided one argument, but there are two parameters? How do you expect that to work? You either need to provide another argument, or remove one parameter.

您提供了一个参数,但有两个参数?你希望它如何运作?您要么需要提供另一个参数,要么删除一个参数。

(I'd also strongly advise that you remove the inprefix from all of your parameters, and look into a real unit testing framework such as JUnit, rather than writing an enormous mainmethod.)

(我还强烈建议您in从所有参数中删除前缀,并查看真正的单元测试框架,例如 JUnit,而不是编写庞大的main方法。)

回答by Jayanth

Also if you like to specify only the Customer Name, you could do so by overloading the method as

此外,如果您只想指定客户名称,则可以通过将方法重载为

    public void setShippingDest(String inCustName)
    {
      return  setShippingDest(inCustName, defaultvalue1);
    }

See how to set default method argument values?

查看如何设置默认方法参数值?