当前位置: 技术问答>java相关
scjp上的一道题
来源: 互联网 发布时间:2015-02-19
本文导语: What is the result of attempting to compile and run the following program 1. public class Test { 2. public void method(Object o) { 3. System.out.println("Object Version"); 4. } 6. System.out.println("String Version"); 7. } 8. public static v...
What is the result of attempting to compile and run the following program
1. public class Test {
2. public void method(Object o) {
3. System.out.println("Object Version");
4. }
6. System.out.println("String Version");
7. }
8. public static void main(String args[]) {
9. Test test = new Test();
10. test.method(null);
11. }
12. }
Select one correct answer
a. The code does not compile.
b. The code compiles cleanly and shows "Object Version".
c. The code compiles cleanly and shows "String Version".
d. The code throws an Exception at Runtime.
Answer: c
why?
1. public class Test {
2. public void method(Object o) {
3. System.out.println("Object Version");
4. }
6. System.out.println("String Version");
7. }
8. public static void main(String args[]) {
9. Test test = new Test();
10. test.method(null);
11. }
12. }
Select one correct answer
a. The code does not compile.
b. The code compiles cleanly and shows "Object Version".
c. The code compiles cleanly and shows "String Version".
d. The code throws an Exception at Runtime.
Answer: c
why?
|
http://www.csdn.net/expert/Topic/300/300131.shtm
http://www.csdn.net/expert/topic/324/324293.shtm
http://www.csdn.net/expert/topic/324/324293.shtm
|
这是面向对象的基本思想!
就是说:
如果B是A子类,即B比A特殊。
如果有参数C类型是B,当然应当首先匹配B参数,因为B比A更具体、更特殊、更接近实体本身。
就是说:
如果B是A子类,即B比A特殊。
如果有参数C类型是B,当然应当首先匹配B参数,因为B比A更具体、更特殊、更接近实体本身。