JavaSE reflection-variable length parameters

Java self-learning route

Variable length parameter

  • JDK5 provides variable length parameters
  • grammar
类型...变量名
  • There can be only one variable length parameter, and it must be the last position in the parameter list
  • If the fixed parameter and variable length parameter method can be matched at the same time, the fixed parameter method will be matched first
  • Variable length parameters can be treated as an array
public class Test {
    
    
	public static void main(String[] args) {
    
    
		l();
		l(1);
		l(3,34);
		
		z(0);
		z(2, "lv");
		z(5, "may","day");
		
		j("lv","may","day");
		String[] m = {
    
    "1","2","3"};
		j(m);
		j(new String[] {
    
    "a","b","c"});
	}
	
	public static void l(int...k) {
    
    
		System.out.println("lzjfw");
	}
	
	// 可变长参数只能有1个,且必须在参数列表中的最后一个位置上
//	public static void z(int...a,String...w) {
    
    
//		
//	}
	public static void z(int a,String...w) {
    
    
		
	}
	
	// 可变长参数可以当做一个数组来看待
	public static void j(String...r) {
    
    
		for(int i = 0;i < r.length;i++) {
    
    
			System.out.println(r[i]);
		}
	}
}

Guess you like

Origin blog.csdn.net/LvJzzZ/article/details/108976477