Java虚拟机在重载时是通过参数的静态类型而不是实际类型作为判定依据的。
Human man = new Man();
Human被称为变量的静态类型,Man称为变量的实际类型
1 class DisPatch{ 2 static class QQ{} 3 static class SonQQ extends QQ{} 4 static class _360{} 5 public static class Father{ 6 public void choice(QQ arg){ 7 System.out.println("F qq"); 8 } 9 public void choice(SonQQ arg){ 10 System.out.println("F SonQQ"); 11 } 12 public void choice(_360 arg){ 13 System.out.println("F 360"); 14 } 15 } 16 public static class Son extends Father{ 17 public void choice(QQ arg){ 18 System.out.println("Son qq"); 19 } 20 public void choice(SonQQ arg){ 21 System.out.println("Son SonQQ"); 22 } 23 public void choice(_360 arg){ 24 System.out.println("Son 360"); 25 } 26 } 27 public static void main(String[] args){ 28 Father f = new Father(); 29 Father son = new Son(); 30 // f.choice(new QQ());//F qq 31 // son.choice(new QQ());//Son QQ 32 QQ q = new SonQQ(); 33 // f.choice(q);//F qq 34 // son.choice(q);//Son QQ 35 f.choice(new SonQQ());//F SonQQ 36 son.choice(new SonQQ());//Son SonQQ 37 } 38 }
浙公网安备 33010602011771号