JDK 1.5 Characteristics Example 2 - VARARGS

xiaoxiao2021-03-06  78

Code Example 1. Varargs.java

Import java.text.MessageFormat;

Import java.util.date;

/ **

* @Author Mao Xiang

*

* When the parameter is a collection or array, you can use ... Simplify definitions

* /

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) {// parameter definition 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);

}

}

转载请注明原文地址:https://www.9cbs.com/read-108573.html

New Post(0)