public class Dec26
{
public static void main(String[] args){
short a1=6;
new Dec26().go(a1);
new Dec26().go(new Integer(7));
}
void go(Short x){
System.out.print("S ");
}
void go(Long x){
System.out.print("L ");
}
void go(int x){
System.out.print("i ");
}
void go(Number x){
System.out.print("N ");
}
}
最后结果是
i N谁能告诉我为什么?
{
public static void main(String[] args){
short a1=6;
new Dec26().go(a1);
new Dec26().go(new Integer(7));
}
void go(Short x){
System.out.print("S ");
}
void go(Long x){
System.out.print("L ");
}
void go(int x){
System.out.print("i ");
}
void go(Number x){
System.out.print("N ");
}
}
最后结果是
i N谁能告诉我为什么?
总结:对于外覆类优先查找本类的函数,如果没有就查找其超类。如果也没有就拆箱找其对应的基本类型,如果没有就扩充。如Integer->Number->int->long
对于基本数据类型,首先会查找对应的基本类型,没有就扩充,没有就自动装箱找其对应的外覆类,没有就调用Number
如 int->long->Integer->Number,由于基本类型不会自动扩充并装箱,所以不能转成Long