在Java5 中提供了变长参数(varargs),也就是在方法定义中可以使用个数不确定的参数,对于同一方法可以使用不同个数的参数调用,例如print("hello");print("hello","lisi");print("hello","张三", "alexia");下面介绍如何定义可变长参数 以及如何使用可变长参数。
1. 可变长参数的定义
使用...表示可变长参数,例如
print(String... args){
...
}
在具有可变长参数的方法中可以把参数当成数组使用,例如可以循环输出所有的参数值。
print(String... args){
for(String temp:args)
System.out.println(temp);
}
2. 可变长参数的方法的调用
调用的时候可以给出任意多个参数也可不给参数,例如:
print();
print("hello");
print("hello","lisi");
print("hello","张三", "alexia")
3. 可变长参数的使用规则
3.1 在调用方法的时候,如果能够和固定参数的方法匹配,也能够与可变长参数的方法匹配,则选择固定参数的方法。看下面代码的输出:
package com; // 这里使用了静态导入
import static java.lang.System.out; public class VarArgsTest { public void print(String... args) {
for (int i = 0; i < args.length; i++) {
out.println(args[i]);
}
} public void print(String test) {
out.println("----------");
} public static void main(String[] args) {
VarArgsTest test = new VarArgsTest();
test.print("hello");
test.print("hello", "alexia");
}
}
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPgAAABMCAIAAADP6O0/AAACNUlEQVR4nO3dQXKqQBSGUdfVC3I9vRo2w2LMRBGlaZBS817+c0YpUpEMPqmbpG/ldIEAp9/+BuAbhE4EoRNB6EQQOhE6oQ/n05Pz8Jev85d5ohNB6EQQOhGETgShE0HoRBA6EYROBKETQehEEDoRhE6E1dDHWl4+7nQ7LlXquOs6fEvviT7WcuBc31hLM+i16/AFQifCVui1luWh7XG6eDqVuvyq/aHfX2jxOvBGG6FPc/U803mxYy1Pg/z+0Gu5T+1jLVrnc3aPLsP5GvT8cd7a0Nkb+mIwGs5GGz7lUOjdR6/Q+Qe9HvrlsphWnr9q9+gyvzCcjS58zMbv0a9zybRPfA38cb/4Gujry8iPU5AVZT7HX0aJIHQiCJ0IQieC0IkgdCIInQhCJ8Kh0C1S8L85/kR/1/ny4ewNw8f9fujwBd3QZ6dUlk03Qm8uZNwuljpOHz8dmVnbMTUd8S790IfpnNXyxGL/NOLDQsZ4W7EYa1kc3mq8Ybr3hQP2PtGXpwsb58s7CxnXzzYe0K3Qe/eFA9ZDf1ySmx1Hv3/+OfT1A+XTu2B7BNq6LxzQDf1xzt4xurSjnO+DzvdE26+zdV84oDe6zH8mLGWaIjr/1a2xkNFc4Ch17LzOyn3hOH8ZJYLQiSB0IgidCEIngtCJIHQiCJ0IQieC0IkgdCIInQhCJ4LQiSB0IgidCEIngtCJIHQiCJ0IQieC0IkgdCIInQhCJ4LQiSB0IgidCEIngtCJIHQi/ABvACclY6uKBQAAAABJRU5ErkJggg==" alt="" />
3.2 如果要调用的方法可以和两个可变参数匹配,则出现错误,例如下面的代码:
package com; // 这里使用了静态导入
import static java.lang.System.out; public class VarArgsTest1 { public void print(String... args) {
for (int i = 0; i < args.length; i++) {
out.println(args[i]);
}
} public void print(String test,String...args ){
out.println("----------");
} public static void main(String[] args) {
VarArgsTest1 test = new VarArgsTest1();
test.print("hello");
test.print("hello", "alexia");
}
}
对于上面的代码,main方法中的两个调用都不能编译通过,因为编译器不知道该选哪个方法调用,如下所示:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAhMAAABwCAIAAAArPmWuAAAS+klEQVR4nO2dz2veRhrH9VcU9g9Y6Ppqlg1zaHsJpVtKYRtT6tKSEiM2h2SdbC7vXt5QKLs+RT7kshAwJjcfcoogvpgXcmgNCcQHX5RuakqCwfSQ0GxvO3vQr2dmnhlp3levfvn74aF19Eozj6TR89U8I2mC0zf/g8FgMBisvgWdewCDwWCwYRmUAwaDwWB+BuWAwWAwmJ9BOWAwGAzmZ1AOGAwGg/kZlAMGg8FgfhZIAAAAwAcoBwAAAD+gHAAAAPyAcgAAAPADygEAAMCPLpQjeXA3WJsGa3ux+dvLmVibBmvTYG0aHrIb3o1etuHkInTlp7Pes+jWNFibBrdmSctuWYmEiHrjDEsSiSAIgoD4ySzqlCQSIppnqyAIQuYKBKAOXfU5Dvd45cg4DjtQDr7SOd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M9s9OpHyRB7dyZbsPlJXLu10fzKHP/Nt5TYoR3tAOWCwIdiT/a3J/mn696PdIrI3YFQ5iirynzLBeLK/ldbIKMfR7jxKNt9WboNytAeUAwYbglHlyMPu6f2tzTu7u3nOp+gfbN0/zTahHQjtn6Tk3XR9UzlKywN9sXK5XKn09P6W1mvRnKy5VeYP6WOd3t8qSygETNkpKEd7QDlgsCGY1ueY7J9mAbcM0Jlg5Imgozt5iulEyhP9n4w5lKOOqKT+5KsVf1Mn629l9qtY5VB3CsrRHlAOGGwIpoxz0Nv5NJie7k82ySjF5u6j7EY+lxP9n4zRcQ5FJ9yZpfJXNY4f7V7ZPdIifu2tju7orrLKoe4UlKM9oBww2BBMyVZlpikH26U4uqOkgLR/KsZ3LI52r7g7K10qh7pTUI72gHLAYEOwCuVQcj5H9/dPT+TRo0JUtvaf6P9kqjCVoxgYd1mNbJVTOaqzVY/2959Q9073J/nQjrJTUI72gHLAYEOwKuWgCavyCdo09aQ9dGt7hUJXDi0DZpMQJZdVjNIrYuBUDnYrSYfN9Yzc1v5940niO0fIVrUJlAMGg43DoBztAeWAwWDjMChHe0A5YDDYOAzK0R5QDhgMNg6DcrTHBgAAjIUGYyOUAwAAgB9QDgAAAH5AOQAAAPgB5QAAAODH/wHSIonLpfhjsQAAAABJRU5ErkJggg==" alt="" />
3.3 一个方法只能有一个可变长参数,并且这个可变长参数必须是该方法的最后一个参数
以下两种方法定义都是错误的。
public void test(String... strings,ArrayList list){
}
public void test(String... strings,ArrayList... list){
}
4. 可变长参数的使用规范
4.1 避免带有可变长参数的方法重载:如3.1中,编译器虽然知道怎么调用,但人容易陷入调用的陷阱及误区
4.2 别让null值和空值威胁到变长方法,如3.2中所示,为了说明null值的调用,重新给出一个例子:
package com;public class VarArgsTest1 { public void print(String test, Integer... is) { } public void print(String test,String...args ){ } public static void main(String[] args) {
VarArgsTest1 test = new VarArgsTest1();
test.print("hello");
test.print("hello", null);
}
}
这时会发现两个调用编译都不通过:
aaarticlea/png;base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vgmFQg2iIvum/YL9/rdVEUayN1iVRtuz9plNZYPg/0wahTpZiuabGhXLQdJ+wTQL+2yYRsWyVypWezr13zeVDYQTCnWS9H/uFq4ML3Rb6wAAAABJRU5ErkJggg==" alt="" />
因为两个方法都匹配,编译器不知道选哪个,于是报错了,这里同时还有个非常不好的编码习惯,即调用者隐藏了实参类型,这是非常危险的,不仅仅调用者需要“猜测”该调用哪个方法,而且被调用者也可能产生内部逻辑混乱的情况。对于本例来说应该做如下修改:
public static void main(String[] args) {
VarArgsTest1 test = new VarArgsTest1();
String[] strs = null;
test.print("hello", strs);
}
4.3 覆写变长方法也要循规蹈矩
下面看一个例子,大家猜测下程序能不能编译通过:
package com; public class VarArgsTest2 { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// 向上转型
Base base = new Sub();
base.print("hello"); // 不转型
Sub sub = new Sub();
sub.print("hello");
} } // 基类
class Base {
void print(String... args) {
System.out.println("Base......test");
}
} // 子类,覆写父类方法
class Sub extends Base {
@Override
void print(String[] args) {
System.out.println("Sub......test");
}
}
答案当然是编译不通过,是不是觉得很奇怪?
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAASEAAAAoCAIAAAA+B9swAAAGoElEQVR4nO2cz2sbRxTH5+/RtafORcE99NCDKUgnF9KQdg49FWqIdFofWloolNEhhB4Mhh4sorQqKdm2oZFrExIwPbRUFYatcSE1cW0ldlwITi3zepj9MbM7q92VZ1e29n1YhDQ7OzsazXfem9l5IuBxdHT02pcJrwY5OTkxWdwsUrWuTnBWTq9aV9McBuuMRCH+u0SBHR0dGbwxagwpCYHG0I4hSB6gHUOQfEE7hiD5gnYMQfIF7RiC5AvaMQTJl0nsWL/daDQajXZfKWm/xxsBoZNRSqsxTgkhlDuXtfyLRsrvy6mUx+GU8pzrFZDZju33eKPdh36b9/aVkvZ7PFFYEjOmMU6JC6WMMDshc74ayLv8AjDcnjYjqqjsxELNMel8DDUmYbNgIHU4JZdKY07QnQOmq1Hj7alRV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Fc/+sv777T0AOP39/a25+eHKKgDs8VsAMFxZTdBYQrRLmBnTWH5xyq5vyVl0z6sfTuiFl3mnwp/l3P52W682cmyavkIai9gUXdmBGoE1L3GHQ5NDjbienp8usonECg1/wUwaW6b6crT1iau83BwFDHbh52P3v24+/uP5vdWPtubmtxeYENgev7W9wJI11m5zfzd+ktxmSWN5xykrgRyh/DaT9GLbaqhiuAeJyjmas+PsWEz8VRQxHRJmRFbIMlVsXYWFr0qpsWUapOxwjVy1FBbvHIeyzwMAPrnzKwC8/cHnWTUmFjvcxP0eL1eMZo5xygn5bRbXTfQa869U/b8EjaVztxRf0fa0JBuxqIlLrzEHaqo415jqFsZQWLxzHGE79vFXP3XtjXc+/HQCX1GNJSvrumIOccrj8hejsdR2TK+xsQanAI1Nd9kjbMe++/mXt95tdn98GFrzePoZT1rzgB6XIqHLZMdMxim7YYhKr1Dyh/RgTGOy7xr905lUlkCvMYBlOm4tJJOvKIuqmcpXLC7eOY7wvnufuLX7MXHQ8t+/lep/gk3GKes1JuePrGyEbxxZxBBn/GRmK+/DF6WLI44gJmMVaX1PPLMSNGVfkeoSJR8yLj3kdqZawyww3jmO2H33eTNLGsub6a/1X4AV8MkoMt45DowfuwRwSoEsBZ+LfC/V4aI900um2HjnODB+7KLDKQGyRAhxu36Rr4gJ0I5deKZlwVBmhkA7hiD58j9UdNfd7zE6fwAAAABJRU5ErkJggg==" alt="" />
第一个能编译通过,这是为什么呢?事实上,base对象把子类对象sub做了向上转型,形参列表是由父类决定的,当然能通过。而看看子类直接调用的情况,这时编译器看到子类覆写了父类的print方法,因此肯定使用子类重新定义的print方法,尽管参数列表不匹配也不会跑到父类再去匹配下,因为找到了就不再找了,因此有了类型不匹配的错误。
这是个特例,覆写的方法参数列表竟然可以与父类不相同,这违背了覆写的定义,并且会引发莫名其妙的错误。
这里,总结下覆写必须满足的条件:
(1)重写方法不能缩小访问权限;
(2)参数列表必须与被重写方法相同(包括显示形式);
(3)返回类型必须与被重写方法的相同或是其子类;
(4)重写方法不能抛出新的异常,或者超过了父类范围的异常,但是可以抛出更少、更有限的异常,或者不抛出异常。
最后,给出一个有陷阱的例子,大家应该知道输出结果:
package com; public class VarArgsTest {
public static void m1(String s, String... ss) {
for (int i = 0; i < ss.length; i++) {
System.out.println(ss[i]);
}
} public static void main(String[] args) { m1("");
m1("aaa");
m1("aaa", "bbb");
}
}