1**2* Created by Lenovo on 2017/12/10.3*variable length parameters in Java4*/5 Public classreflect04 {6 7 //M1 has a comparable variable length parameter of type int8 //M1 The argument can be 0-n when called9 Public Static voidM1 (int... a) {TenSYSTEM.OUT.PRINTLN ("int variable-length parameter execution ...." +a); One } A - //If there is a method that can be precisely matched, the method is called, and the variable-length parameter is no longer executed. - Public Static voidM1 (inta) { theSYSTEM.OUT.PRINTLN ("Exact match execution ..." +a); - } - - //variable-length parameters can be equated as an array + Public Static voidm2 (String ... args) { - for(inti=0;i<args.length;i++){ +System.out.println ("M2 all like the sport as follows:" +args[i]); A } at } - - Public Static voidM3 (Class ... args)throwsexception{ - - for(inti=0;i<args.length;i++){ -Class arg =Args[i]; in System.out.println (Arg.newinstance ()); - } to } + - //variable-length parameters can only occur once, and can only appear in the last digit of all parameters the //Error instance: * //Public static void M4 (String ... args,int i) {} $ Public Static voidMain (string[] args)throwsException {Panax Notoginseng - //Executive M1 theM1 (1);//call the M1 method for exact matching +M1 (); AM1 (A); theM1 (1,2,3,4); + - //Executive M2 $M2 ("Travel", "See the movie", "Swim", "play"); $ - //Executive M3 -M3 (Date.class, Employlee.class); the - //M1 OutputWuyi //M1 Precision output: Exact match execution ..... 1 the //int type variable long parameter execution .... [[Email protected] - //int type variable long parameter execution .... [[Email protected] Wu //int type variable long parameter execution .... [[Email protected] - About //m2 Output $ //m2 are fond of sports as follows: Travel - //m2 are fond of the following sports: watching movies - //m2 are fond of sports as follows: Swimming - //m2 are fond of sports as follows: Play A + //m3 Output the //Sun Dec 21:59:58 CST - //NULL parameter construction method execution ... .. $ //employlee{name= ' null '} the } the}
Variable length parameters in Java