可变个数的形参

 1 /*
 2  * 可变个数形参的方法
 3  * 1.jdk 5.0新增的内容
 4  * 2.具体使用:
 5  *     2.1 可变个数形参的格式:数据类型 ... 变量名
 6  *     2.2 当调用可变个数形参的方法时,传入的参数的个数可以是:0个,1个,2个...
 7  *     2.3可变个数形参的方法与本类中方法名相同,形参不同的方法之间构成重载。
 8  *  2.4可变个数形参的方法与本类中方法名相同,形参类型也相同的数组之间不构成重载。即二者不可共存。
 9  *  2.5可变个数形参在方法中的形参中,必须声明在末尾。
10  *  2.6可变个数形参在方法中的形参中,最多只能声明一个可变形参。
11  */
12 public class MethodArgs {
13 
14     public static void main(String[] args) {
15         MethodArgs test = new MethodArgs();
16         test.show(12);
17         // test.show("hell0");
18         // test.show("hello","world");
19         // test.show();
20 
21         test.show(new String[] { "AA", "BB", "CC" });
22     }
23 
24     public void show(int i) {
25 
26     }
27 
28     // public void show(String s){
29     // System.out.println("show(String)");
30     // }
31     public void show(String... strs) {
32         System.out.println("show(String ...strs)");
33 
34 
35         for (int i = 0; i < strs.length; i++) {
36             System.out.println(strs[i]);
37         }
38     }
39 
40     // 此方法与上一方法不可共存
41     // public void show(String[] strs){
42     //
43     // }
44 
45     public void show(int i, String... strs) {
46 
47     }
48 
49     //The variable argument type String of the method show must be the last parameter
50 //    public void show(String... strs,int i,) {
51 //
52 //    }
53 }
posted @ 2021-12-08 20:45  行呗  阅读(47)  评论(0)    收藏  举报