日期:2014-05-20  浏览次数:20674 次

谁能做对这道题,我就拜他为师...大神请进(跑代码作弊无视之)
做出答案者请给出原因


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));
}


------解决方案--------------------
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)方法
}
------解决方案--------------------
Java code

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类的方法
}