日期:2014-05-20  浏览次数:20675 次

可变参数重载问题
Java code

public static void pp(int b){
    System.out.println("hellow!");
        
}
public static void pp(int... b){
    System.out.println("hellow sec!");
}




但执行pp(3)时如何判断调用哪个方法呢!!

------解决方案--------------------
Java code

public class VarArgsTest {
    public static void main(String[] args) {
        m1();
        m1("one");
        m1("one","two");
        //m1("one","two","three");//报错,因为系统不知道调用哪个方法...
        
    }
    
    public static void m1(String s1,String s2){
        System.out.println("m1(String s1,String s2)");
    }
    public static void m1(){
        System.out.println("m1()");
    }
    public static void m1(String s){
        System.out.println("m1(String s)");
    }
    public static void m1(String s,String... str){
        System.out.println("m1(String s1,String... str)");
    }
    public static void m1(String... str){
        System.out.println("m1(String... str)");
    }

}