Java Generics– 这个语法是什么?

Java Generics– 这个语法是什么?,第1张

概述以下代码的这部分是什么<String,Void,Bitmap>意思?我甚至都不知道甚至调用了这种语法.privateclassDownloadImageTaskextendsAsyncTask<String,Void,Bitmap>{}这是原始代码(从这里找到:http://developer.android.com/guide/components/processes-and-threads.html):pu

以下代码的这部分是什么< 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由3种不同的类型描述,String作为第一个参数,VoID作为第二个参数,Bitmap作为第三个参数,当您使用AsyncTask时.

这在java中称为Generics,从Java5开始引入.请阅读此tutorial以了解有关泛型的更多信息.关于androID AsyncTasktask如何使用泛型,这是javadoc.

更新:来自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 Generics – 这个语法是什么?全部内容,希望文章能够帮你解决Java Generics – 这个语法是什么?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1101743.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存