做出答案者请给出原因
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));
}
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将一个字符串中的所有的http链接都加入<a href="url">url</a>标签
- Statement只能有一个ResultSet的问题
- Java Thread的 关于 join()方法的一个小问题
- 怎么让JPanel变化
- Color中的getColor(string)怎么用啊
- 如何用Java写一个简易的多线程的HTTP Web Server
- java1.5容器类继承问题
- JAI怎么压缩tiff啊,急救
- Vector中他的元素可以是Vector型的吗?
- 求助配置maven时没有src和webapp目录
- 请教一个线程同步的问题(在线等)
- 没有被明显的调用或引用然而注释以后与注释前的结果却不同,请给予指点!
下面的相同
show(y)部分,y声明成什么类型而不管其实际类型,编译时候调用相应的overload方法
编译时根据变量类型(包括方法调用者变量和方法参数变量的类型确定调用何种方法,记住不是实际类型,而是变量指定类型,因为它是编译时决定)。运行时根据实际调用方法的对象中查找参数和名称都对的上的覆盖的子类方法
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)方法
}
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原因大家自己分析下!!!
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
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类的方法
}