java Java泛型中的3个点是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17623217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What do the 3 dots in Java generics mean?
提问by maysi
for example I have a code like this: (from here)
例如我有一个这样的代码:(从这里)
private class LongOperation extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {}
@Override
protected void onPostExecute(String result) {}
@Override
protected void onPreExecute() {}
@Override
protected void onProgressUpdate(Void... values) {
}
}
and what do the 3 dots in the parameter of the method do?
方法参数中的 3 个点是做什么的?
回答by William Morrison
The three dots are referred to as varargs
and here, allow you to pass more than one string to the method like so:
这三个点被称为varargs
和 在这里,允许您将多个字符串传递给方法,如下所示:
doInBackground("hello","world");
//you can also do this:
doInBackground(new String[]{"hello","world"});
Within the method doInBackground
you can enumerate over the varargs variable, params
like so:
在该方法中,doInBackground
您可以枚举 varargs 变量,params
如下所示:
for(int i=0;i<params.length;i++){
System.out.println(params[i]);
}
So its basically an array of strings within the scope of doInBackground
所以它基本上是一个范围内的字符串数组 doInBackground
回答by chancea
The compiler treats the three dots ...
as taking in an array of that object. In this case String
and Void
. The amount of objects you pass in is the size of the array.
编译器将三个点...
视为接收该对象的数组。在这种情况下String
和Void
。您传入的对象数量是数组的大小。
Thus:
因此:
doInBackground("Hi", "Hello", "Bye")
will create an array of String
of length 3.
doInBackground("Hi", "Hello", "Bye")
将创建一个String
长度为 3的数组。