<!--[endif]-->
Varargs、printf()、static import
Varargs
方法參數中如果有不定數量的參數,以前的版本要使用該方法前需要將參數放入一個數組中,然後將該數組傳入方法的參數中。如:
Object[] arguments = {new Integer(7),new Date(),"a disturbace in the Force"};
String result = MessageFormat.format("at {1,time} on {1,date},there was {2} on planet {0,number,integer}",arguments)
<!--[if !supportEmptyParas]--> <!--[endif]-->
現在可以用下面的聲明方法:
public static String format(String patter, Object... arguments);
"..."表明最後一個參數可能以數組形式或順序傳入參數。只能用於最後一個參數。
String result = MessageFormat.format("xxxx",new Integer(7),new date(),"ssss");
對此種方法不應重載。重載之後很難分辨哪個方法被調用。
<!--[if !supportEmptyParas]--> <!--[endif]-->
printf()
public PrintStream printf(String format,Object... args)=
out.format(format,args);
<!--[if !supportEmptyParas]--> <!--[endif]-->
<!--[if !supportEmptyParas]--> <!--[endif]-->
static import
如果要使用某個類聲明的靜態成員,可以在"import"部分直接引入該類的靜態成員。在使用時不再需要聲明類作限定符。
import static java.lang.Math.*;
<!--[if !supportEmptyParas]--> <!--[endif]-->
double r = cos(PI*2);
一般情況下要慎用該功能,濫用該功能會導致代碼難以閱讀且不易維護。
建議引入時僅引入你常用的那個常量,也就是說不要引入所有常量。