2009年2月6日 星期五

第39題

Given:
11. public class ItemTest{
12. private final int id;
13. public ItemTest(int id){this.id = id;}
14. public void updateId(int newId){id = newId;}
15.
16. public static void main(String[] args){
17. ItemTest fa = new ItemTest(42);
18. fa.updateId(69);
19. System.out.println(fa.id);
20. }
21. }
Which four statments are true? (Choose four.)
A. Compilation fails.
B. An exception is thrown at runtime.
C. The attribute id in the ItemTest object remains unchanged.
D. The attribute id in the ItemTest object is modified to the new value.
E. A new ItemTest object is created with the preferred value in the id attribute.

------------------ ------------------------------------------------------------------
答案:A
參考:6-5 繼承


12. private final int id;
13. public ItemTest(int id){this.id = id;} //正確,建構式可改寫final 變數
14. public void updateId(int newId){id = newId;} //錯誤,不是建構式卻想改寫final 變數

沒有留言:

張貼留言