1. class A ...{
2. public String show(D obj)...{
3. return ("A and D");
4. }
5. public String show(A obj)...{
6. return ("A and A");
7. }
8. }
9. class B extends A...{
10. public String show(B obj)...{
11. return ("B and B");
12. }
13. public String show(A obj)...{
14. return ("B and A");
15. }
16. }
17. class C extends B...{}
18. class D extends B...{}
(二)问题:以下输出结果是什么?
19. A a1 = new A();
20. A a2 = new B();
21. B b = new B();
22. C c = new C();
23. D d = new D();
24. System.out.println(a1.show(b)); ①
25. System.out.println(a1.show(c)); ②
26. System.out.println(a1.show(d)); ③
27. System.out.println(a2.show(b)); ④
28. System.out.println(a2.show(c)); ⑤
29. System.out.println(a2.show(d)); ⑥
30. System.out.println(b.show(b)); ⑦
31. System.out.println(b.show(c)); ⑧
32. System.out.println(b.show(d)); ⑨ 最后一个不明白
2. public String show(D obj)...{
3. return ("A and D");
4. }
5. public String show(A obj)...{
6. return ("A and A");
7. }
8. }
9. class B extends A...{
10. public String show(B obj)...{
11. return ("B and B");
12. }
13. public String show(A obj)...{
14. return ("B and A");
15. }
16. }
17. class C extends B...{}
18. class D extends B...{}
(二)问题:以下输出结果是什么?
19. A a1 = new A();
20. A a2 = new B();
21. B b = new B();
22. C c = new C();
23. D d = new D();
24. System.out.println(a1.show(b)); ①
25. System.out.println(a1.show(c)); ②
26. System.out.println(a1.show(d)); ③
27. System.out.println(a2.show(b)); ④
28. System.out.println(a2.show(c)); ⑤
29. System.out.println(a2.show(d)); ⑥
30. System.out.println(b.show(b)); ⑦
31. System.out.println(b.show(c)); ⑧
32. System.out.println(b.show(d)); ⑨ 最后一个不明白
解决方案 »
- linux下在后台得到<input>框值的奇怪问题
- 程序插入Oracle的DATE字段只有年月日
- AJAX anywhere的删除出现问题了!
- apache soap
- 在red hat enterprise 6中装java ee6 sdk说未找到主机名……
- struts编写的B/S程序使用tomcat做服务器有时会无法访问????急!!!!
- 项目马上要开始了,要在春节前完成,但是人员比较紧,考虑用struts,有什么好的模式?
- 哪里有的下载适合初学struts的教程?最好有中文翻译的.
- 怎样在struts中实现??
- 求解啊啊啊
- 在不同的两个JSP页面如何传递 " List" 的问题?
- eclipse在线安装maven的问题
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 & A
System.out.println(a1.show(c));//A & A
System.out.println(a1.show(d));//A & D
System.out.println(a2.show(b));// B & A
System.out.println(a2.show(c));//B & A
System.out.println(a2.show(d));//A & D
System.out.println(b.show(b)); //B & B
System.out.println(b.show(c)); //B & B
System.out.println(b.show(d)); //A & D
}
}
class A{
public String show(D obj){
return("A & D");
}
public String show(A obj){
return("A & A");
}
}
class B extends A{
public String show(B obj){
return("B & B");
}
public String show(A obj){
return("B & A");
}
}
class C extends B{
}
class D extends B{
}
因为 B 是继承 A 的
B里没有参数为D 的方法 所以向上塑性~~
刚好A里有参数为D的方法就打印出来咯
如果A里没有参数为D的方法
那么传入参数D 因为是继承B 所以会向上塑性
这是 B 理由参数为B 的方法就会被调用~~
这里就是判断 对象向上塑性优先级 高于 传入参数的向上塑性的优先级~~
可以这么理解
小猫 <叫> “喵!”
小狗 <叫> “旺!”
小羊 <叫> “咩!”
这里是 new 出 B 的对象 转换成 A 类型~~
所以 a2 可以使用 B 里的方法
这里的优先级 如果 A B 有同一个 方法 A会覆盖B 的 但是 A 能得到B里的方法~·
你可明白~
就看这句吧,
第一步,a2的类型是A,所以,a2.show()要在A类型中匹配
第二步,A类型里有两个show()方法,是函数重载。现在,传入参数为b(类型B),A里面没有show(B obj),所以b向上转型为A,匹配到show(A obj)。
第三步,由于a2 = new B(),所以这个时候是多态(函数重写)上场,因为B里恰好重写了show(A obj),所以调用的B里的show(A obj)。
好了,剩下的5和6是一回事。这个问题同时考到了函数重写和函数重载。
总结来说,多态是个什么回事呢?就是个动态绑定机制。java程序在运行前并不知道要调用哪个函数(自己?子类?哪个子类?),而是要在运行时通过java自己的方法调用机制找到正确的方法。
多态的掌握还是很重要的。