Java泛型 - 这是什么语法?语法、这是什么、Java、泛型

2023-09-12 06:43:56 作者:十年好唱却难等

什么的code这部分低于<字符串,太虚,位图> 是什么意思?我甚至不知道这是什么语法,甚至被称为。

 私有类DownloadImageTask扩展的AsyncTask<字符串,太虚,位图> {

}
 

原来这里是code(从这里找到:的http:// developer.android.com/guide/components/processes-and-threads.html ):

 公共无效的onClick(视图v){
    新DownloadImageTask()执行(http://example.com/image.png);
}

私有类DownloadImageTask扩展的AsyncTask<字符串,太虚,位图> {
    / **系统调用它来在辅助线程执行的工作,
      *它传递给AsyncTask.execute()的参数* /
    受保护的位图doInBackground(字符串...网址){
        返回loadImageFromNetwork(网址[0]);
    }

    / **系统调用它来在UI线程中执行工作,并提供
      *从doInBackground()的结果* /
    保护无效onPostExecute(位图的结果){
        mImageView.setImageBitmap(结果);
    }
}
 

解决方案

 的AsyncTask<字符串,太虚,位图>
 
Java 泛型

它指出的AsyncTask是由3不同的类型,String作为第一个参数,无效的第二个参数和位图作为第三个参数,当您使用AsyncTask的描述。

这就是所谓的 的泛型在Java中,从Java5中介绍起。请仔细阅读本教程,以了解更多关于泛型。这里是一个如何的Andr​​oid AsyncTasktask使用泛型的javadoc 。

更新:发件人的AsyncTask的javadoc

 1)参数,可以在执行时发送给任务的参数的类型。
2)进展,背景计算过程中发表的进展单元的类型。
3)结果,背景计算的结果的类型。
 

What does this part of the code below <String, Void, Bitmap> mean? I don't even know what this syntax is even called.

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {

}

Here is the original code (Found from here: 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>

Tells AsyncTask is described by 3 distinct types, String as first parameter, Void as second parameter and Bitmap as third parameter, when you use AsyncTask.

This is called Generics in java, introduced from Java5 onwards. Please read this tutorial to understand more about Generics. Here is javadoc on how android AsyncTasktask uses generics.

Update: From 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.