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.
相關文章
- mysql insert的特殊用法MySql
- Java中DecimalFormat的用法JavaDecimalORM
- Java中super的用法Java
- JAVA中String format的用法JavaORM
- Java中ThreadLocal的用法和原理Javathread
- java中的HashMap用法總結JavaHashMap
- centos7下別名(alias)的特殊用法CentOS
- Java 8 中Stream用法Java
- java中Lamdba表示式的用法整理Java
- Java 中 this 和 super 的用法詳解Java
- scss 檔案裡的特殊符號 @ 和 @include 的用法CSS符號
- 3個例項介紹shell指令碼中幾個特殊引數的用法指令碼
- java高階用法之:JNA中的FunctionJavaFunction
- java高階用法之:JNA中的StructureJavaStruct
- 文字中的特殊字元字元
- java中printf中用法詳解Java
- java中的Static、final、Static final各種用法Java
- Java 8 中 Map 騷操作之 merge() 的用法Java
- Java8中Stream 的一些用法Java
- java高階用法之:JNA中的回撥Java
- Java 中 this 和 super 的用法概述及異同Java
- setInterval 、 settimeout 、clearInterval 用法(特殊情況下代替schedule)
- Python 中的特殊運算子Python
- python中的特殊方法使用Python
- HTML 中的特殊符號HTML符號
- java特殊編碼生成Java
- 好程式設計師Java教程分享Java中this的幾種用法程式設計師Java
- java高階用法之:JNA中的Memory和PointerJava
- 一道演算法題,引出collections.Counter的特殊用法演算法
- JS 中特殊的物件-陣列JS物件陣列
- Android 中的特殊攻擊面Android
- Windows中的特殊資料夾Windows
- Yaml中特殊符號"| > |+ |-"的作用YAML符號
- 淺析Java中的雜湊值HashCode的作用及用法Java
- Shell命令列中的特殊字元及其轉義(去除特殊含義)命令列字元
- Java 列舉 switch的用法Java
- JAVA的陣列基本用法Java陣列
- Java IdentityHashMap類的用法 | baeldungJavaIDEHashMap
- java——split的用法(字串拆分)Java字串