下面这部分代码是什么<String, Void, Bitmap>
意思是?我什至不知道这种语法叫什么。
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
}
这是原始代码(从此处找到:http://developer.android.com/guide/components/processes-and-threads.html):
public void onClick(View v) {
new DownloadImageTask().execute("http://example.com/image.png");
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
/** The system calls this to perform work in a worker thread and
* delivers it the parameters given to AsyncTask.execute() */
protected Bitmap doInBackground(String... urls) {
return loadImageFromNetwork(urls[0]);
}
/** The system calls this to perform work in the UI thread and delivers
* the result from doInBackground() */
protected void onPostExecute(Bitmap result) {
mImageView.setImageBitmap(result);
}
}
最佳答案
AsyncTask<String, Void, Bitmap>
当您使用 AsyncTask 时,告诉 AsyncTask 由 3 种不同的类型描述,String 作为第一个参数,Void 作为第二个参数,Bitmap 作为第三个参数。
这叫做 Generics在java中,从Java5开始引入。请阅读此tutorial了解更多关于泛型的信息。这是 javadoc关于 android AsyncTasktask 如何使用泛型。
更新:来自 AsyncTask javadoc
1) Params, the type of the parameters sent to the task upon execution.
2) Progress, the type of the progress units published during the background computation.
3) Result, the type of the result of the background computation.
关于Java 泛型——这个语法有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13238150/