class Father{
   public void out(Father father){
      System.out.println("This is Father Father");
   }   public void out(Son son){
      System.out.println("This is Father son");
   }
}class Son extends Father{
   public void out(Father father){
     System.out.println("This is Son Father");
   }   public void out(Son son){
     System.out.println("This is Son Son");
   }
}
public class Text{
   public static void main(String[] arg0){
      Father father=new Son();
      father.out(father);
   }
}这样输出的是This is Son Father.
我是这么理解的,当程序在匹配参数的时候是看引用变量本身,而在匹配哪个类的时候是看他所引用的对象
不知道这么理解有没有错,想请高手解释下多态匹配参数和匹配类的原理,这好像跟静态绑定和动态绑定有关

解决方案 »

  1.   

    你NWE的是new Son(); 
    传递的参数是(father); 
    输出肯定是This is Son Father
      

  2.   

    动态绑定的条件:要有继承;要有重写;父类引用指向子类对象。从这个条件上看,肯定是动态绑定。Son继承了Father类,也重写了Father的两个out()方法。定义了一个父类引用数据类型father变量,这个变量new出了一个子类对象。当调用out(father)方法时,运行时,会根据传递对象的实际类型,调用相应的方法。