标签:
public class ArgsDemo { public static void main(String[] args) { int a = 10; int b = 20; add(a, b); int c = 30; add(a, b, c); System.out.println(addArgs()); System.out.println(addArgs(a)); System.out.println(addArgs(a, b)); System.out.println(addArgs(a, b, c)); System.out.println(addArgs("结果:", b, c)); } // 有0或多个参数,参数在使用时当作数组 private static int addArgs(int... s) { // System.out.println(s.length); int count = 0; for (int i = 0; i < s.length; i++) { count += s[i]; } return count; } // 可变长参数之后不能在跟参数,一个方法只能有一个可变参数 private static void addArgs(boolean b,boolean... booleans/* ,int... s */) { } // 至少有两个参数 private static int addArgs(int a, int b, int... s) { System.out.println("至少有两个参数"); int count = a + b; for (int i = 0; i < s.length; i++) { count += s[i]; } return count; } // 有1个或多个参数 private static String addArgs(String str, int... is) { int count = 0; for (int i = 0; i < is.length; i++) { count += is[i]; } return str + count; } private static int add(int a, int b, int c) { return a + b + c; } private static int add(int a, int b) { return a + b; } }
标签:
原文地址:http://www.cnblogs.com/syousetu/p/5762687.html