下面这一题答案是什么啊,因为我觉得标准答案好像是错的,所以先把题目发出来看看大家的答案是什么。Given:
23. int z = t;
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(stuff4);}
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

解决方案 »

  1.   

    选C吧
    26行不合适:不要对public方法的参数断言
    33行不合适:调用了会改变程序状态的方法
    29合适:在程序最不大可能到达的地方断言
    31合适:断言private方法的参数
      

  2.   

    33行代码错了,应该是assert(stuff4())
      

  3.   

    dreamover(梦醒了〖http://hellfire.cn〗) 

    断言还真没用过,感觉应该是26和31行比较合适
    .............................26行肯定不适合使用断言,因为是公共方法里面。