<span style="font-size:18px;">匹配顺序: this.show(O), super.show(O), this.show(super(O)), super.show(super(O)) 1)先确保参数完全匹配O的前提下,依次匹配this与super。 2)再考虑参数用super(O)渐近匹配O,依次匹配this与super 3)当存在子类覆盖父类方法时,根据new子类实例的原则,先调子类方法。 public class TestDT{ public static void main(String args[]){ A a1 = new A(); A a2 = new B(); B b = new B(); C c = new C(); D d = new D(); System.out.println(a1.show(b)); // ① A and A System.out.println(a1.show(c)); // ② A and A System.out.println(a1.show(d)); // ③ A and D System.out.println(a2.show(b)); // ④ B and A System.out.println(a2.show(c)); // ⑤ B and A System.out.println(a2.show(d)); // ⑥ A and D System.out.println(b.show(b)); // ⑦ B and B System.out.println(b.show(c)); // ⑧ B and B System.out.println(b.show(d)); // ⑨ A and D }</span> }
版权声明:本文为博主原创文章,未经博主允许不得转载。希望大家多指教指教
原文地址:http://blog.csdn.net/yangxin_blog/article/details/49679081