JAVA中...的特殊用法。
今天在學習Android的AsyncTask類的時候,看到doInBackground(Params... params)方法是這樣定義的,但是裡面的Params... params是什麼意思呢,經過查詢終於弄清楚了其中的奧祕,原來是一個可變長的引數,裡面可以自定義型別,在Java官方文件有過介紹。
In past releases, a method that took an arbitrary number of values required you to create an array and put the values into the array prior to invoking the method. For example, here is how one used the MessageFormat class to format a message:
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);
It is still true that multiple arguments must be passed in an array, but the varargs feature automates and hides the process. Furthermore, it is upward compatible with preexisting APIs. So, for example, the MessageFormat.format method now has this declaration:
public static String format(String pattern, Object... arguments);
The three periods after the final parameter's type indicate that the final argument may be passed as an array or as a sequence of arguments. Varargs can be used only in the final argument position. Given the new varargs declaration for MessageFormat.format, the above invocation may be replaced by the following shorter and sweeter invocation:
String result = MessageFormat.format(
"At {1,time} on {1,date}, there was {2} on planet "
+ "{0,number,integer}.",
7, new Date(), "a disturbance in the Force");
There is a strong synergy between autoboxing and varargs, which is illustrated in the following program using reflection:
// Simple test framework
public class Test {
public static void main(String[] args) {
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);
}
}
This little program is a complete, if minimal, test framework. It takes a list of class names on the command line. For each class name, it instantiates the class using its parameterless constructor and invokes a parameterless method called test. If the instantiation or invocation throws an exception, the test is deemed to have failed. The program prints each failure, followed by a summary of the test results. The reflective instantiation and invocation no longer require explicit array creation, because the getMethod and invoke methods accept a variable argument list. The program also uses the new [printf](http://docs.oracle.com/javase/1.5.0/docs/api/java/io/PrintStream.html#printf(java.lang.String, java.lang.Object...)) facility, which relies on varargs. The program reads much more naturally than it would without varargs.
So when should you use varargs? As a client, you should take advantage of them whenever the API offers them. Important uses in core APIs include reflection, message formatting, and the new printf facility. As an API designer, you should use them sparingly, only when the benefit is truly compelling. Generally speaking, you should not overload a varargs method, or it will be difficult for programmers to figure out which overloading gets called.
相關文章
- C++ 中特殊的用法C++
- java replaceall 用法:處理特殊字元Java字元
- CSS中position樣式的一些特殊用法CSS
- mysql insert的特殊用法MySql
- Gradle特殊用法Gradle
- python中星號變數的幾種特殊用法Python變數
- Java中super的用法Java
- Java中DecimalFormat的用法JavaDecimalORM
- java中filter的用法JavaFilter
- java中switch的用法Java
- java中Scanner的用法。Java
- 星號變數的特殊用法變數
- C#中ref、out及特殊說明引數的用法C#
- JAVA中subString的用法Java
- java中Varargs用法Java
- JavaScript中圓括號()和方括號[]的一個特殊用法JavaScript
- Java的特殊類用法:檔案類、內部類、本地類、匿名類Java
- JAVA中String format的用法JavaORM
- java中的HashMap用法總結JavaHashMap
- java中this關鍵字的用法Java
- Java反射以及在Android中的特殊應用Java反射Android
- Java中的兩個特殊變數this和super (轉)Java變數
- java中Lamdba表示式的用法整理Java
- Java 中 this 和 super 的用法詳解Java
- Java中ThreadLocal的用法和原理Javathread
- java中String的21種用法Java
- Java中Split函式的用法技巧Java函式
- Oracle 樹形結構查詢的特殊用法Oracle
- 3個例項介紹shell指令碼中幾個特殊引數的用法指令碼
- java高階用法之:JNA中的FunctionJavaFunction
- java高階用法之:JNA中的StructureJavaStruct
- java中native關鍵字的用法Java
- java中final修飾符的用法Java
- scss 檔案裡的特殊符號 @ 和 @include 的用法CSS符號
- java中printf中用法詳解Java
- Java中jar命令用法詳解JavaJAR
- HTML 中的特殊符號HTML符號
- CSS中的特殊符號CSS符號