编程语言

img maoxiang

JDK 1.5 特性使用实例2--Varargs

发表于2004/10/10 11:41:00  890人阅读

 

代码实例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);

}

}

阅读全文
0 0

相关文章推荐

img
取 消
img