简体   繁体   English

为什么 Netbeans 看不到我的意思是我的哪个可变参数方法?

[英]Why doesn't Netbeans see which of my varargs-method I mean?

The code has a problem in line 18 n.fun1("11",1);代码在第 18 行有问题 n.fun1("11",1); in netbeans,but not in other IDE this problem only in NetBeans 7.0.1 the IDE shows:在 netbeans 中,但不在其他 IDE 中,此问题仅在 NetBeans 7.0.1 中显示:

reference to fun1 is ambiguous, both method fun1(java.lang.String,java.lang.Object...) in Test and method fun1(java.lang.String,int,java.lang.Object...) in Test match reference to fun1 is ambiguous, both method fun1(java.lang.String,java.lang.Object...) in Test and method fun1(java.lang.String,int,java.lang.Object...) in Test匹配

The corresponding code is here:对应的代码在这里:

interface Test {
    public void fun1(String str, Object... objs);
    public void fun1(String str, int i, Object... objs);
}
public class NewClass implements Test {
    public void fun1(String str, Object... objs) {
        System.out.println("111111111111111111111");
    }
    public void fun1(String str, int i, Object... objs) {
        System.out.println("2222222222222");
    }
    public static void main(String[] args) {
        Test n = new NewClass();
        n.fun1("11", 1);
    }
}

There is no such method in your interface fun1("11", 1); // fun1(String, int)您的界面fun1("11", 1); // fun1(String, int) fun1("11", 1); // fun1(String, int) , try this: fun1("11", 1); // fun1(String, int) ,试试这个:

n.fun1("11", new Integer(1));

I faced the same issue.我遇到了同样的问题。 I found it to be already reported to Netbeans team here: https://netbeans.org/bugzilla/show_bug.cgi?id=200024我发现它已经在这里报告给 Netbeans 团队: https://netbeans.org/bugzilla/show_bug.cgi?id=200024

Your code should compile with JDK6, but not with JDK7.您的代码应该使用 JDK6 编译,而不是使用 JDK7。 According to Netbeans folks, the Java spec does not allow these two methods in a single class.根据 Netbeans 人员的说法,Java 规范不允许在单个 class 中使用这两种方法。 In their opinion, the fact that JDK6 does not complain about it should be considered as a bug, that has been fixed in JDK7在他们看来,JDK6 没有抱怨它的事实应该被认为是一个 bug,已经在 JDK7 中修复

That's why they answered they would not remove (or make optional) this feature in Netbeans.这就是为什么他们回答他们不会在 Netbeans 中删除(或使可选)此功能。 Even if it looks a bit inconsistent when using JDK6.即使使用JDK6时看起来有点不一致。

In my case, we renamed one of the two methods, so that to be JDK7-compliant.在我的例子中,我们重命名了这两种方法之一,以便与 JDK7 兼容。

Updated更新

After rereading your question I can see the real problem here.重读您的问题后,我可以在这里看到真正的问题。 There are two fun1 methods in your interface.您的界面中有两个fun1方法。

public void fun1(String str, Object... objs) // first

and

public void fun1(String str, int i, Object... objs); // second

The problem is, where you're calling the method of your class:问题是,您在哪里调用 class 的方法:

n.fun1("11", 1);

The IDE could not decide whether you want to call your first method or your second. IDE 无法决定您是要调用第一种方法还是第二种方法。

Because, it can be the first with autoboxing your 1 parameter to an Integer object.因为,它可能是第一个将您的1参数自动装箱为Integer object。 Or it can be the second, with the objs parameter as an empty array.或者它可以是第二个,将objs参数作为一个空数组。

There is no way to fix this without modifying your interface.如果不修改您的界面,就无法解决此问题。 My java compiler (1.6.0_26) can compile it without any problem and it calls your second method.我的 java 编译器(1.6.0_26)可以毫无问题地编译它,它会调用您的第二种方法。

But as the IDE says it's ambiguous and it is right about that, this definition is confusing and should be avoided.但正如 IDE 所说的模棱两可,而且这是正确的,这个定义令人困惑,应该避免。

Which JDK is your netbeans using?您的 netbeans 使用的是哪个 JDK? Could it be an old (really old) one?会不会是旧的(真的很旧)?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我看不到我的项目在 netbeans 中运行? - Why can't I see my project running in netbeans? netbeans 7.3为什么看不到我已经安装了Java 1.7.0_21? - Why doesn't netbeans 7.3 see that I've installed Java 1.7.0_21? 为什么Java varargs不支持集合? - Why doesn't Java varargs support collections? 在Java 7中使用方法重载时,为什么autoboxing不会覆盖varargs? - Why doesn't autoboxing overrule varargs when using method overloading in Java 7? 为什么slf4j的Logger没有同时接受消息的varargs和异常的方法? - Why doesn't slf4j's Logger has a method that accepts both varargs for the message and an exception? 日食。 如果没有无法访问代码的方法没有调用,为什么我看不到编译错误? - Eclipse. Why don't I see compile error if method with unreachable code doesn't invoke? 为什么我的jar文件不在netbeans之外运行? - why doesn't my jar file run outside netbeans? 当我在Netbeans中更改模板时,为什么什么都没发生? - When I change templates in Netbeans, why doesn't anything happen? 当我尝试在NetBeans中打开我的项目时,它无法打开 - When I try to open my project in NetBeans it doesn't open 为什么 jUnit 5 看不到我的异常测试? - Why jUnit 5 doesn't see my exception test?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM