Question 44
Click the Exhibit button. 
1. public class A { 
2. public String doit(int x, inty) { 
3. return “a”
4. } 
5. 
6. public String doit(int... vals) { 
7. return “b!”
8. } 
9. } 
Given: 
25. Aa=newA(); 
26. System.out.println(a.doit(4, 5)); 
What is the result? 
A. Line 26 prints “a” to System.out
B. Line 26 prints !(R)b” to System.out
C. An exception is thrown at line 26 at runtime. 
D. Compilation of class A will fail due to an error in line 6. 
Answer: A问题:如何判断什么时候调用doit(int x,int y)而什么时候调用doit(int …vals)呢???
谢谢

解决方案 »

  1.   

    当参数都满足时优先调用doit(int x,int y),参数不满足doit(int x,int y)时,若满足doit(int... vals),(参数个数为0或多个)则调用doit(int... vals),
      

  2.   

    当参数刚好是两个的时候,调用最适合的那个方法,这是Java函数调用的原则。比如 public void paint(Object ibj){
    }
    public void paint(String str){
    }和上面是一个道理
      

  3.   

    当参数刚好是两个的时候,调用最适合的那个方法,这是Java函数调用的原则。比如 public void paint(Object ibj){
    }
    public void paint(String str){
    }和上面是一个道理
      

  4.   

    java方法的重写,主要根据参数的个数和参数的类型来判断
    public String doit(int x, inty)
    {
       return “a”
    }
    public String doit(int... vals) 

       return “b!”
    } 先匹配参数类型,然后看参数的个数,调用对应的方法
      

  5.   

    class Alien {
    String invade(short ships) { return "a few"; }
    String invade(short... ships) { return "many"; }
    }
    class Defender {
    public static void main(String [] args) {
    System.out.println(new Alien().invade((short)7));
     
    } }
    相同的The String invade(short... ships) can also match the call, but it doesn't do so exactly ... so if you removed invade(short ships) method then invade(short... ships) would would be invoked.Does that clear things up?