2017.12.13T19_B2_5mianshiti

package com.xdf.exam;

public class A {

 public String show(D d) {   return " A  ADN  D";  }

 public String show(A a) {   return " A  ADN  A";  }

***************************

package com.xdf.exam;

public class B extends A {

 public String show(B b) {   return " B  ADN  B";  }

 public String show(A a) {   return " B  ADN  A";  }

*****************

package com.xdf.exam;

public class Demo {

 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("以下的输出结果:");   /**    * A类中有方法show(A a)    * B是A的子类    */   System.out.println(a1.show(b)); // A ADN A   /**    *  A类中有方法show(A a)    *  C类的父类是B类    *  B类的父类是A类    */   System.out.println(a1.show(c));// A ADN A   System.out.println(a1.show(d));// A ADN D   /**    * a2是父类的引用指向了子类B的对象    * 01.先去A类中查询show()并且参数是B类型的    * 02.发现没有 但是有show(A a)    * 03.因为B类中重写了show(A a)    *     所以执行B类的show(A a)    */   System.out.println(a2.show(b));// B ADN A   System.out.println(a2.show(c));// B ADN A   System.out.println(a2.show(d));// A ADN D   System.out.println(b.show(b));// B ADN B   System.out.println(b.show(c));// B ADN B   System.out.println(b.show(d));// A ADN D

原文地址:https://www.cnblogs.com/xiaoxiao1016/p/8031555.html