加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 功能参数中3点的含义是什么?

发布时间:2020-12-14 05:09:42 所属栏目:Java 来源:网络整理
导读:这个问题在这里已经有一个答案: Java,3 dots in parameters8 我正在阅读Android文档中的AsyncTask. private class DownloadFilesTask extends AsyncTaskURL,Integer,Long { protected Long doInBackground(URL... urls) { int count = urls.length; long to
这个问题在这里已经有一个答案:> Java,3 dots in parameters8
我正在阅读Android文档中的AsyncTask.
private class DownloadFilesTask extends AsyncTask<URL,Integer,Long> {
     protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader.downloadFile(urls[i]);
             publishProgress((int) ((i / (float) count) * 100));
             // Escape early if cancel() is called
             if (isCancelled()) break;
         }
         return totalSize;
     }

     protected void onProgressUpdate(Integer... progress) {
         setProgressPercent(progress[0]);
     }

     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     }
 }

问题是doInBackground(URL … urls)这3个点是什么?

解决方法

这不是Android功能.这是一个Java功能(添加在Java 5中),因此您可以使用“自定义”参数.

这种方法:

protected Long doInBackground(URL... urls) {
  for (URL url : urls) {
    // Do something with url
  }
}

和这个:

protected Long doInBackground(URL[] urls) {
  for (URL url : urls) {
    // Do something with url
  }
}

对于内部的方法是一样的.
整个不同之处在于您调用的方式.
对于第一个(也称为varargs),您可以简单地做到这一点:

doInBackground(url1,url2,url3,url4);

但是对于第二个,你必须创建一个数组,所以如果你试图在一行中做到这一点就像:

doInBackground(new URL[] { url1,url3 });

好的是,如果您尝试使用这种方式调用使用varargs编写的方法,它将以与否(后向支持)相同的方式工作.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读