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

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航