【发布时间】:2017-03-23 13:41:09
【问题描述】:
原始人又来了,打破了规则,我以前学过。好吧,在技术上不是原始的,而是由它们组成的。
我了解到,只要没有比 rest 更具体的方法,编译时错误就会发生在这里。
public static void caller(){
z5(); // Error. Neither Integer, nor String is more specific
z5(null); // Error for the same reason
}
public static void z5(Integer...integers){
System.out.println("Integer z5 called");
}
public static void z5(String...strings){
System.out.println("String z5 called");
}
现在图元出现了。
public static void caller(){
z1(null); // Error cuz [I, [J, [F all are subclass of Object.
z1(); // SURPRISINGLY works and calls the int one. WHY?
}
public static void z1(int...integers){
System.out.println("int z1 called");
}
public static void z1(long...longs){
System.out.println("long z1 called");
}
public static void z1(float...floats){
System.out.println("float z1 called");
}
此处出现预期的编译时错误。
public static void caller(){
z1(null); // Error
z1(); // Error
}
public static void z1(int...integers){
System.out.println("int z1 called");
}
public static void z1(boolean...bools){
System.out.println("bool z1 called");
}
现在我的问题是,int[]、float[] 或任何原始数组都不是原始类型,那么为什么它们的处理方式与其他引用类型不同?
--更新--
@john16384你不认为我读过你的“可能重复”Varargs in method overloading in Java
那里的最佳答案是 您不能将 var-args 与加宽或装箱结合使用。除了我忘了提,OP 的代码贴在那里,在我的 jdk 7 上运行良好。
究竟发生了什么,适用于 (int...is) & (float...fs) 但不适用于 (Integer...is) & (Float. ..fs) 而不是 (int...is) & (boolean...bool)
【问题讨论】:
标签: java arrays overloading variadic-functions primitive