scjp

wfz6366 2008-11-11 10:16:33
given:
Question 67
Given:
23. int z=5;
24.
25. public void stuff1(int x) {
26. assert (x> 0);
27. switch(x) {
28. case 2: x= 3;
29. default: assert false; } }
30.
31. private void stuff2(int y) { assert (y < 0); }
32.
33. private void stuff3() { assert (stuff4O); }
34.
35. private boolean stuff4() { z = 6; return false; }
Which is true?
A. All of the assert statements are used appropriately.
B. Only the assert statement on line 31 is used appropriately.
C. The assert statements on lines 29 and 31 are used appropriately.
D. The assert statements on lines 26 and 29 are used appropriately.
E. The assert statements on lines 29 and 33 are used appropriately.
F. The assert statements on lines 29, 31, and 33 are used
appropriately.
G. The assert statements on lines 26, 29, and 31 are used
appropriately.

Answer: C

不明白的是第29行。29行正确吗?书上有句原话说:即使在公共方法内,也不要断言检查从不会发生的情况。这句话应该怎么理解呢!?觉得好像跟这个选项有点联系。
书上给了例子:
switch(x){
case 1:y=3;
case 2: y=9;
case 3: y=27;
default:assert false;

并且解释说,在这个例子中,我们么有执行布尔测试,我们已经断言从不应该到达哪里,因此,只要凭到达哪里就是我们度断言/假设的自动失败。
这个例子也不明白。
...全文
73 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
meadking 2008-11-12
  • 打赏
  • 举报
回复
代码有可能不会执行到达
meadking 2008-11-12
  • 打赏
  • 举报
回复
即使在公共方法内,也不要断言检查从不会发生的情况

"程序逻辑和业务"不要依赖断言!
wfz6366 2008-11-12
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 xblue3 的回复:]
代码有可能不会执行到达
[/Quote]
那default:assert false;这句是对的了?

即使在公共方法内,也不要断言检查从不会发生的情况。这句话应该怎么理解?有没有例子说明一下?

50,545

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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