SCJP 1.6版考題 042

出自 陳富國維基館
前往: 導覽搜尋
1. class A{
2.   public String doit(int x, int y){
3.     return "a";
4.   }
5.
6.   public String doit(int... vals){
7.   return "b";
8.   }
9. }
Given:
25. A a = new A();
26. System.out.println(a.doit(4, 5));


What is the result?

  A. Line 26 prints "a" to System.out.
  B. Line 26 prints "b" to System.out.
  C. An exception is thrown at runtime.
  D. Compilation of class A will fail due to an error in line 6. 


解答


Ans: A

解說:

方法呼叫代入二個整數,方法是以“明確”的二個整數參數宣告的方法為優先選取。

 
若要輸出b(選擇第6行),則建議的程式可改為
class A{ 
   public String doit(int x, int y, int c){ 
     return "a"; 
   }  

   public String doit(int... vals){ 
     return "b"; 
   }
   public static void main(String[] args){
    A a = new A(); 
     System.out.println(a.doit(4, 5)); 
   }
 }