【问题标题】:what is the meanining of 3 dots in function parameters? [duplicate]函数参数中的 3 个点是什么意思? [复制]
【发布时间】:2014-04-18 04:31:58
【问题描述】:

我正在阅读 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) 这三个点是什么?

【问题讨论】:

    标签: java android android-asynctask


    【解决方案1】:

    这不是 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
      }
    }
    

    对于内部方法都是一样的。 整个区别在于您调用它的方式。 对于第一个(也称为可变参数),您可以简单地这样做:

    doInBackground(url1,url2,url3,url4);
    

    但是对于第二个你必须创建一个数组,所以如果你尝试在一行中这样做,它会像:

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

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

    【讨论】:

      【解决方案2】:

      这意味着这个函数接受可变数量的参数 - 它的arity 是可变的。例如,所有这些都是有效的调用(假设这绑定到 AsyncTask 的实例):

      this.doInBackground();                 // Yes, you could pass no argument
                                             // and it'd be still valid.
      this.doInBackground(url1);
      this.doInBackground(url1, url2);
      this.doInBackground(url1, url2, url3);
      // ...
      

      在你的方法体内,urls 基本上是一个数组。关于 varargs 的一个有趣的事情是,您可以通过两种方式调用此类方法 - 通过传递参数数组或通过将每个 url 指定为单独的方法参数。所以,这两个是等价的:

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

      您可以使用 for 循环遍历所有这些:

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

      你当然可以自己定义类似的方法,例如:

      public void addPerson (String name, String... friends) {
        // ...
      }
      

      在此示例中,您的方法将接受一个强制参数(name,您不能省略这个)和可变数量的friends 参数:

      this.addPerson();               // INVALID, name not given
      this.addPerson("John");         // Valid, but no friends given.
      this.addPerson("John," "Kate"); // Valid, name is John and one friend - Kate
      

      并不是说这个结构从 Java 5 开始就可用。你可以找到文档here

      【讨论】:

      • 你能进一步解释一下吗?
      • 是指只有3个变量还是任意数量的变量?
      • 这意味着你可以传递零个或多个参数。
      【解决方案3】:

      【讨论】:

      • 这可能是一个可能的评论,而不是一个答案!
      【解决方案4】:

      这三个点意味着可以将可变数量的参数传递给我们使用 progress[0] 或 progress[1] 等访问的函数。

      【讨论】:

        【解决方案5】:

        这三个点表示函数的可变长度参数。

        【讨论】:

        • 是指只有3个变量还是任意数量的变量?
        • 表示零个或多个元素的数组。
        猜你喜欢
        • 2011-07-08
        • 1970-01-01
        • 2012-12-10
        • 2014-02-18
        • 2013-10-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多