![0347850ae4ed5d84a3b84494e5bf074d.png](https://i-blog.csdnimg.cn/blog_migrate/f0493f9dc082507fdf3030314507780b.png)
点击上方 蓝字关注我们 !
![ae5f428a9c84c4aa238a6d9c258f2d60.png](https://i-blog.csdnimg.cn/blog_migrate/fe3a10c652bae99ab26e378b1d528579.png)
方法---可变个数的形参
JDK 5.0以前:采用数组形参来定义方法, 传入多个同一类型变量
public static void show(int a ,String[] b);
JDK5.0之后:采用可变个数形参来定义方法, 传入多个同一类型变量
public static void show(int a ,String…b);
怎么样用呢?
声明格式:方法名(参数的类型...参数名)
可变参数:方法参数部分指定类型的参数个数是可变多个(可以为0个)。
可变个数形参的方法与同名的方法之间, 彼此构成重载。
可变参数方法的使用与方法参数部分使用数组是一致的。
方法的参数部分有可变形参, 需要放在形参声明的最后。
方法的形参位置,最多可声明一个可变个数形参。
代码展示:
public class Test { public void show(String[] arr){ System.out.println("含字符串数组参数的方法 "); } public void show(String b){ System.out.println("与可变形参方法构成重载的方法"); } public void show1(String... b) { System.out.println("形参长度可变的方法"); } public static void main(String[] args) { Test t = new Test(); // t.show1(); t.show("1"); // t.show(new String[] {"123","456"}); }}