Java泛型 - 这是什么语法? [英] Java Generics - What is this syntax for?

查看:105
本文介绍了Java泛型 - 这是什么语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么的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<字符串,太虚,位图>
 

它指出的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.

这篇关于Java泛型 - 这是什么语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆