代码实例1。 Varargs.java
import java.text.MessageFormat;
import java.util.Date;
/**
* @author 毛翔
*
* 当参数 为集合或者数组时,可以采用… 简化定义
*/
public class Varargs {
public static void main(String[] args) {
Test1();
Test2(args);
}
public static void Test1(){
Object[] arguments = {
new Integer(7),
new Date(),
"a disturbance in the Force"
};
String result = MessageFormat.format(
"At {1,time} on {1,date}, there was {2} on planet "
+ "{0,number,integer}.", arguments);
System.out.println(result);
String result1 = MessageFormat.format(
"At {1,time} on {1,date}, there was {2} on planet "
+ "{0,number,integer}.",
7, new Date(), "a disturbance in the Force");
System.out.println(result1);
}
public static void Test2(String... args) { //参数的定义varargs
int passed = 0;
int failed = 0;
for (String className : args) {
try {
Class c = Class.forName(className);
c.getMethod("test").invoke(c.newInstance());
passed++;
} catch (Exception ex) {
System.out.printf("%s failed: %s%n", className, ex);
failed++;
}
}
System.out.printf("passed=%d; failed=%d%n", passed, failed);
}
}
本文地址:http://com.8s8s.com/it/it14811.htm