Java 编译错误与可变参数
我使用以下方法创建了一个库:
protected static int foo(String strParam, Object... params)
我将此库链接到我的应用程序并调用该方法:
foo("a","b")
但它无法编译,我收到以下错误消息: 实际参数 String 无法通过方法调用转换转换为 Object[]。
有什么想法吗?
I created a library with the following method :
protected static int foo(String strParam, Object... params)
I link this library to my application and I call the method :
foo("a","b")
but it does not compile, I get the following error message :
actual argument String cannot be converted to Object[] by method invocation conversion.
any idea ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对我有用:
那么:
Works for me:
So:
foo
somewhere (i.e. overloaded methods)?该方法受
保护
。调用是发生在同一个包中的类中,还是发生在子类中?如果不是,调用者将无法看到foo
的定义。但我不明白为什么它会导致该特定错误。这可能表明编译器正在尝试将其与不同的方法签名相匹配。您的调用真的只是
foo( ... )
吗?我希望在那里看到包含类的名称,因为它是一个静态方法 - 即LibraryClass.foo( ... )
。如果您没有限定方法名称,那么它应该在本地类范围中查找匹配的声明。The method is
protected
. Is the call occurring in a class in the same package, or in a subclass? If not, the caller cannot see this definition offoo
. I don't see why it would result in that particular error though. This might be evidence that the compiler is trying to match this up with a different method signature.Is your call really just
foo( ... )
? I would expect to see the name of the containing class there, since it is a static method -- i.e.LibraryClass.foo( ... )
. If you are not qualifying the method name, then it should be looking in the local class scope for a matching declaration.