做出答案者请给出原因
class A {

public String show(D obj) {
return ("A and D");
} public String show(A obj) {
return ("A and A");
}
} class B extends A {


public String show(B obj) {
return ("B and B");
} public String show(A obj) {
return ("B and A");
}
} class C extends B {
public String show(D obj) {
return ("C and D");
}
} class D extends B {
}
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();
B e = new C();
System.out.println(a1.show(b));
System.out.println(a1.show(c));
System.out.println(a1.show(d));
System.out.println(a2.show(b));
System.out.println(a2.show(c));
System.out.println(a2.show(d));
System.out.println(b.show(b));
System.out.println(b.show(c));
System.out.println(b.show(d));
System.out.println(a2.show(a1));
System.out.println(b.show(a2));
System.out.println(b.show(a1));
System.out.println(b.show(a1));
System.out.println(e.show(d));
}

解决方案 »

  1.   

    b是一个从A继承而来的对象 当它传进去的时候当做一个A对象自然调用public String show(A obj)
    下面的相同
      

  2.   

    x.show(...)参考多态部分,x实际哪个类型,就是调用哪个中声明的方法。
    show(y)部分,y声明成什么类型而不管其实际类型,编译时候调用相应的overload方法
      

  3.   

    java调用方法过程如下:
    编译时根据变量类型(包括方法调用者变量和方法参数变量的类型确定调用何种方法,记住不是实际类型,而是变量指定类型,因为它是编译时决定)。运行时根据实际调用方法的对象中查找参数和名称都对的上的覆盖的子类方法
    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();
    B e = new C();
    System.out.println(a1.show(b));A and A b是B类对象也是A类对象
    System.out.println(a1.show(c));A and A c是C类对象也是A类对象
    System.out.println(a1.show(d));A and D d是D类对象
    System.out.println(a2.show(b));B and A 调用A类的show(A obj)方法被B类方法覆盖
    System.out.println(a2.show(c));A and D 调用A类的show(D obj)方法
    System.out.println(a2.show(d));B and B d是C类对象也是B类对象
    System.out.println(b.show(b));B and B b是B类对象
    System.out.println(b.show(c));B and B c是C类对象也是B类对象
    System.out.println(b.show(d));B and B d是C类对象也是B类对象
    System.out.println(a2.show(a1));B and A 调用A类的show(A obj)方法被B类方法覆盖
    System.out.println(b.show(a2));B and A 调用B类的show(A obj)方法
    System.out.println(b.show(a1));B and A 调用B类的show(A obj)方法
    System.out.println(b.show(a1));B and A 调用B类的show(A obj)方法
    System.out.println(e.show(d));B and B 调用B类的show(B obj)方法
    }
      

  4.   

    正确答案应为:
    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));A and D 正确应为 :B and A
    System.out.println(a2.show(d));B and B 正确应为 :B 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));B and B 正确应为 :B and D
    System.out.println(a2.show(a1));B and A 
    System.out.println(b.show(a2));B and A 
    System.out.println(b.show(a1));B and A 
    System.out.println(b.show(a1));B and A 
    System.out.println(e.show(d));B and B 正确应为 :C and D原因大家自己分析下!!!
      

  5.   

    楼主想要的答案我在这篇帖子里回答了:http://topic.csdn.net/u/20110923/12/2f5fcfe4-4484-40f7-8a44-ee53ce91ac17.html
      

  6.   

    更正正确答案(最终版):
    a1.show(b) : A and A
    a1.show(c) : A and A
    a1.show(d) : A and D
    a2.show(b) : B and A
    a2.show(c) : B and A
    a2.show(d) : A and D
    b.show(b) : B and B
    b.show(c) : B and B
    b.show(d) : A and D
    a2.show(a1) : B and A
    b.show(a2) : B and A
    b.show(a1) : B and A
    b.show(a1) : B and A
    e.show(d) : C and D
      

  7.   


    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();
    B e = new C();
    System.out.println(a1.show(b));//A and A,b是A的子类,就是说B也是一个A,调用class A的show(A obj)方法
    System.out.println(a1.show(c));//理由同上
    System.out.println(a1.show(d));//传来一个D对象,当然调用的是A类的show(D obj)方法
    System.out.println(a2.show(b));//注意这里,它首先在A类的方法里找到可以匹配的函数,然后发现子类重写了该方法,就调用子类,也就是B类的 show(A obj)方法,注意B类的 show(B obj),它是调用不了的,因为这是B类的特有方法,在A类里没有这个方法,也就没有重写
    System.out.println(a2.show(c));//理由同上
    System.out.println(a2.show(d));//传来一个D对象,当然调用的是A类的show(D obj)方法System.out.println(b.show(b));//调用B类的show(B obj)方法
    System.out.println(b.show(c));//调用B类的show(B obj)方法System.out.println(b.show(d));//调用B类从A类继承的show(D obj)方法
    System.out.println(a2.show(a1));//a2是父类引用指向子类对象,子类又重写了改方法,所以调用的是子类B的show(A obj)方法
    System.out.println(b.show(a2));//a2是一个A类的对象,所以调用的是B类的show(A obj) 方法
    System.out.println(b.show(a1));//理由同上
    System.out.println(b.show(a1));//理由同上
    System.out.println(e.show(d));//B类从A类继承的show(D obj)又被子类C重写了,e是父类引用指向子类对象,所以最终调用的是C类的方法
    }