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.
答案:A
參考:8-6 Java列舉類型
---------------------------------------------
20行先呼叫Title裡的MR, MR是列舉類型Title裡的屬性.
MR會呼叫建構子14行, 傳入"Mr.".
之後在乎叫format(String last, String first).
沒有留言:
張貼留言