1 class Human{ 2 } 3 4 class Man extends Human{ 5 } 6 7 class Woman extends Human{ 8 } 9 10 public class Test { 11 12 void show(Human h) { 13 System.out.println("Human"); 14 } 15 16 void show(Man m) { 17 System.out.println("Man"); 18 } 19 20 void show(Woman w) { 21 System.out.println("Women"); 22 } 23 24 public static void main(String [] argv) { 25 Human a = new Man(); 26 Human b = new Woman(); 27 Human c = new Human(); 28 Test test = new Test(); 29 test.show(a); 30 test.show(b); 31 test.show(c); 32 } 33 }
以上程序的输出应该全是"Human",首先确定这是重载而没有涉及到多态,程序会根据调用方法的参数类型来确定应该调用哪一个方法,这是在编译期间就需要确定的,而以上的a,b,c的静态类型(声明类型)是唯一能在编译时确定的,所以会有这样的输出,如果注释掉第一个show方法,编译则不能通过,提示找不到方法的原型。