知道为什么吗?

xiaming 2001-09-24 12:03:43
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.

请详细解释:
...全文
126 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
咚咚咚咚咚咚 2001-09-24
  • 打赏
  • 举报
回复
我想如果用null的时候,先要知道他的类型,如果不能确定就强制转换类型;
至于题目吗,我想对null作method()判断时,应该自动作类型转换,但是无论如何同时存在String 和 StringBuffer编译器强制类型不能选择通过,也就是我们编程序的思路应该改变(应该在调用方法的时候进行强制类型转换。

以上是我对本题目的猜想
咚咚咚咚咚咚 2001-09-24
  • 打赏
  • 举报
回复
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
咚咚咚咚咚咚 2001-09-24
  • 打赏
  • 举报
回复
为什么?我不懂!很有趣!
ender 2001-09-24
  • 打赏
  • 举报
回复
TO:Norwaywoods(挪威的森林)
你的看法基本上是对的……
实际上这种情况下,满足条件的多个方法如果不具有继承关系,则编译器就会报错,和是否同一等级无关……
但为什么是匹配最“特殊”的参数呢?SUN对此有说明吗?你是在哪里知道的呢?
hexiaofeng 2001-09-24
  • 打赏
  • 举报
回复
gz
Norwaywoods 2001-09-24
  • 打赏
  • 举报
回复
首先,我可以告诉xiaming兄,SCJP考试,不会考这么容易的题。
以下是解答:
这是一个OverLoad的问题(不是OverRiding),当你传入NULL时,编译器会自动匹配最"特殊"的参数,这里特殊是指String派生自Object,所以,String比Object特殊,(你应该明白吧!这是面向对象的基本思想),第二题为什么错,是因为StringBuffer与String都是同一等级的,因此,传入Null时,编译器无法辨别,So it's wrong !!!
wilddragon 2001-09-24
  • 打赏
  • 举报
回复
关注
咚咚咚咚咚咚 2001-09-24
  • 打赏
  • 举报
回复
俺也不懂,也许有高手民明白,或问问sun的人吧
ender 2001-09-24
  • 打赏
  • 举报
回复
嗯,比较怪,研究一下……
xiaming 2001-09-24
  • 打赏
  • 举报
回复
为什么同时存在String 和 StringBuffer编译器强制类型不能选择通过?
hailong326 2001-09-24
  • 打赏
  • 举报
回复
gz

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧