SCJP 1.6版考題 103

出自 陳富國維基館
於 2013年3月30日 (六) 05:39 由 Ikk (對話 | 貢獻) 所做的修訂 (新頁面: Given: 11. public enum Title{ 12. MR("Mr."), MRS("Mrs."), MS("Ms."); 13. private final String title; 14. private Title(String t){title = t;} 15. public String format(Stri...)
(差異) ←上個修訂 | 最新修訂 (差異) | 下個修訂→ (差異)
前往: 導覽搜尋
Given:
11. public enum Title{
12.   MR("Mr."), MRS("Mrs."), MS("Ms.");
13.   private final String title;
14.   private Title(String t){title = t;}
15.   public String format(String last, String first){
16.     return title + " " + first + " " + last;
17.   }
18. }
19. public static void main(String[] args){
20.   System.out.println(Title.MR.format("Doe", "John"));
21. }


What is the result?
  A. Mr. John Doe
  B. An exception is thrown at runtime.
  C. Compilation fails because of an error in line 12.
  D. Compilation fails because of an error in line 15.
  E. Compilation fails because of an error in line 20.

解答


Ans: A

解說:

可把一個enum型態看成一個類別
Title.MR è new Title(“Mr.”)
Title.MRS è new Title(“Mrs.”)
Title.MS è new Title(“Ms.”)