"SCJP 1.6版考題 103" 修訂間的差異

出自 陳富國維基館
前往: 導覽搜尋
(新頁面: 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...)
 
(無差異)

於 2013年3月30日 (六) 05:39 的最新修訂

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.”)