问一道 测试题 帮忙啊

zjb19870616 2009-02-26 12:20:33
import java.io.*;
class Master {
String doFileStuff() throws FileNotFoundException { return "a"; }
}
class Slave extends Master {
public static void main(String[] args) {
String s = null;
try {
s = new Slave().doFileStuff();
} catch ( Exception x) {
s = "b";
}
System.out.println(s);
}
// insert code here
}

Which, inserted independently at // insert code here, will compile,
and produce the output b? (Choose all that apply.)

A. String doFileStuff() { return "b"; }

B. String doFileStuff() throws IOException ( return "b"; }

C. String doFileStuff(int. x) throws IOException ( return "b"; }

D. String doFileStuff() throws FileNotFoundException { return "b"; }

E. String doFileStuff() throws NumberFormatException { return "b"; }

F. String doFileStuff() throws NumberFormatException, FileNotFoundException { return "b"; }


答案 给的是 A D E F

小弟 刚学 java 大侠们帮忙啊 B为什么不对啊 请大家帮忙啊 也希望有人 给的回答 详细点 让小弟 多学些知识谢谢啊 分不是很多 大家见谅啊 呵呵
...全文
148 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
susoft2008 2009-02-26
  • 打赏
  • 举报
回复
楼上的换句话就是说, 子类抛出的异常必须是父类抛出异常本身或其子类
paullbm 2009-02-26
  • 打赏
  • 举报
回复
这就是继承中的异常问题!!

子类继承了父类之后,覆盖的方法抛出的异常只能小于或等于父类中的方法抛出的异常!!!


IOException 比 FileNotFoundException 要大。所以不能!!
MT502 2009-02-26
  • 打赏
  • 举报
回复
子类只能比父类抛出更少的检查异常,NumberFormatException是runtime异常所以可以抛出。
ZiSheng 2009-02-26
  • 打赏
  • 举报
回复
[Quote=引用楼主 zjb19870616 的帖子:]
小弟 刚学 java 大侠们帮忙啊 B为什么不对啊 请大家帮忙啊 也希望有人 给的回答 详细点 让小弟 多学些知识谢谢啊 分不是很多 大家见谅啊 呵呵
[/Quote]

在java里边,子类重写父类的方法,如果父类的方法有抛出异常的语句,子类的方法抛出的异常必须得是
父类抛出异常的子类,或者和父类抛出的一样;
如果父类没有抛出异常,子类也不能抛出异常;
zhangpeixv 2009-02-26
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 paullbm 的回复:]
这就是继承中的异常问题!!

子类继承了父类之后,覆盖的方法抛出的异常只能小于或等于父类中的方法抛出的异常!!!


IOException 比 FileNotFoundException 要大。所以不能!!
[/Quote]
有道理
传奇 2009-02-26
  • 打赏
  • 举报
回复
上面的都解释了为什么了.

62,634

社区成员

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

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