十一 20

先看一个5.0当中的例子:
public class Test{
         public Test(){}
         public void out(String …args){
             for(String s:args){
                  System.out.println(s);
             }
         }
         public static void main(String[] args){        
              Test t = new Test();
              t.out(“a”,”b”,”c”);
         }
}

可以注意到,在out方法的参数表当中,我们的参数声明是…args,这就是5.0引入的Varargs概念,即允许你一次传达多个参数,同样的效果如果需要在5.0之前实现的话,我们可能需要把方法的参数列表声明为一个数组,或集合,然后在调用该方法时,把参数封装到一个数组或者集合当中。即:

     public void out(String[] args){}
        out(new String[]{“a”,”b”,”c”});


Tags:

作者:Jock

Leave a Reply

You must be logged in to post a comment.

Switch to our mobile site