考虑这个代码:
class Foo {
public void doIt(String... strs) {
System.out.println("this is varargs");
}
private void doIt(String str) {
System.out.println("this is single");
}
}
class Bar {
public static void main (String[] args) {
new Foo().doIt("");
}
}
使用javac版本1.6.0_29,它无法编译,说明:
VarArgsError.java:14: doIt(java.lang.String) has private access in Foo
new Foo().doIt("");
^
1 error
是的,这是愚蠢的代码,至少有两个明显的解决方法,但我很好奇。基于section 15.12.2 of the specification,这个编译错误似乎是javac中的一个错误,因为第一步应该删除非varargs doIt,因为根据section 6.6.1不可访问。我在查询算法中缺少一些其他细节,或者这显然是错误的我认为是