当前位置: 技术问答>java相关
知道为什么吗?
来源: 互联网 发布时间:2015-02-06
本文导语: Q78 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. } 5. public void method(String s) { 6. System.out.println("...
Q78
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. }
5. public void method(String s) {
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
Q79
What is the result of attempting to compile the following program
1. public class Test {
2. public void method(StringBuffer sb) {
3. System.out.println("StringBuffer Verion");
4. }
5. public void method(String s) {
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 correctly and shows "StringBuffer Version".
c. The code compiles correctly and shows "String Version".
d. The code throws an Exception at Runtime
Answer:a.
请详细解释:
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. }
5. public void method(String s) {
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
Q79
What is the result of attempting to compile the following program
1. public class Test {
2. public void method(StringBuffer sb) {
3. System.out.println("StringBuffer Verion");
4. }
5. public void method(String s) {
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 correctly and shows "StringBuffer Version".
c. The code compiles correctly and shows "String Version".
d. The code throws an Exception at Runtime
Answer:a.
请详细解释:
|
首先,我可以告诉xiaming兄,SCJP考试,不会考这么容易的题。
以下是解答:
这是一个OverLoad的问题(不是OverRiding),当你传入NULL时,编译器会自动匹配最"特殊"的参数,这里特殊是指String派生自Object,所以,String比Object特殊,(你应该明白吧!这是面向对象的基本思想),第二题为什么错,是因为StringBuffer与String都是同一等级的,因此,传入Null时,编译器无法辨别,So it's wrong !!!
以下是解答:
这是一个OverLoad的问题(不是OverRiding),当你传入NULL时,编译器会自动匹配最"特殊"的参数,这里特殊是指String派生自Object,所以,String比Object特殊,(你应该明白吧!这是面向对象的基本思想),第二题为什么错,是因为StringBuffer与String都是同一等级的,因此,传入Null时,编译器无法辨别,So it's wrong !!!
|
import java.awt.*;
public class demo
{
public void method(Object o)
{
System.out.println("Object Version");
}
public void method(int i)
{
System.out.println("Int Verion");
}
public void method(StringBuffer sb)
{
System.out.println("StringBuffer Verion");
}
public void method(String s)
{
System.out.println("String Version");
}
public void method(Button b)
{
System.out.println("Button Version");
}
public static void main(String args[])
{
demo demo = new demo();
demo.method((Button)null);
demo.method((StringBuffer)null);
demo.method((String)null);
demo.method((Object)null);
// demo.method((int)null);//this can not passed
demo.method((int)('9'));
}
}
结果:
Button Version
StringBuffer Verion
String Version
Object Version
Int Verion
public class demo
{
public void method(Object o)
{
System.out.println("Object Version");
}
public void method(int i)
{
System.out.println("Int Verion");
}
public void method(StringBuffer sb)
{
System.out.println("StringBuffer Verion");
}
public void method(String s)
{
System.out.println("String Version");
}
public void method(Button b)
{
System.out.println("Button Version");
}
public static void main(String args[])
{
demo demo = new demo();
demo.method((Button)null);
demo.method((StringBuffer)null);
demo.method((String)null);
demo.method((Object)null);
// demo.method((int)null);//this can not passed
demo.method((int)('9'));
}
}
结果:
Button Version
StringBuffer Verion
String Version
Object Version
Int Verion
|
TO:Norwaywoods(挪威的森林)
你的看法基本上是对的……
实际上这种情况下,满足条件的多个方法如果不具有继承关系,则编译器就会报错,和是否同一等级无关……
但为什么是匹配最“特殊”的参数呢?SUN对此有说明吗?你是在哪里知道的呢?
你的看法基本上是对的……
实际上这种情况下,满足条件的多个方法如果不具有继承关系,则编译器就会报错,和是否同一等级无关……
但为什么是匹配最“特殊”的参数呢?SUN对此有说明吗?你是在哪里知道的呢?
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。