java自动装箱概念SCJP
这个问题是在实验室里被操纵的
public class B{
public static void main(String args[]){
byte g =10;
Byte x = new Byte(g);
B p= new B();
p.doStuff(x);
p.doStuff(g);
}
void dostuff(Number y){
System.out.println("C");
}
void doStuff(short t){
System.out.println("X");}
}
输出:XC
在上面的程序中,我不明白为什么它将“C”作为输出
因为字节-->;字节-->;短
但如果是,字节g=10;它应该再次通过相同的方法,并给出“X”作为输出
public class B{
public static void main(String args[]){
byte b1=12;
Byte B1=new Byte(b1);
capture(b1);
capture(B1);
}
static void capture(short x){
System.out.print("A");
}
static void capture(Number x){
System.out.print("G");
}
}
输出:银
在这里,我也理解了为什么它是A,因为short大于byte,所以它给出了A
但是,对于其他输出,其应再次为A,因为B1是字节-->;字节(自动装箱)---->;通过短(魏德宁)。为什么是G
# 1 楼答案
不要认为这是理所当然的,但我认为这是因为字节是一个数字,所以如果它选择了那个版本,就不必进行任何自动装箱。而且,如果它仍然选择了另一个版本,就根本没有办法拨打这个号码。因此,让这样的调用调用数字版本更有意义,这正是java所做的