【问题标题】:Optional method parameter in JavaJava中的可选方法参数
【发布时间】:2015-08-27 17:11:53
【问题描述】:

我有这个函数httpGet(),它调用http():

public static int httpGet(String url, StringBuilder response) throws IOException {
    return http(url,(http)->http.setRequestMethod("GET"), response);
}

private static int http(String url, httpMethod method, StringBuilder response) throws IOException {
    HttpURLConnection http = (HttpURLConnection)new URL(url).openConnection();
    http.setConnectTimeout(5000);
    http.setReadTimeout(5000);
    method.doMethod(http);
    int status = 404;
    ......
    ......
    return status;
}

我想为readTimeout 添加一个附加参数,该参数必须是可选的,默认值将在其他情况下使用。

在这种情况下,readTimeout 对于所有调用都设置为 5000,但我希望在更长的超时时间内执行此特定调用。

我认为我需要这个新参数是可选的,因为我不想更改调用此 http() 方法的实现。

我是这样称呼它的:

Assert.assertEquals(HTTP_OK, httpGet(DEFAULT_BROWSCAP_ENDPOINT, result));

如何为readTimeout 实现一个新的可选参数?

【问题讨论】:

  • Java 没有默认值,如 Python、C++、VBA、Delphi 和许多语言。使用备用签名创建新的构造函数。
  • 你需要定义一个新方法,(不幸的是?)java不支持可选参数。

标签: java optional-parameters


【解决方案1】:

Java 没有默认值,例如 Python、C++、VBA、Delphi 和许多语言。使用备用签名创建新的构造函数。

public static int httpGet(String url, StringBuilder response) throws IOException {
    return httpGet(URL, response, 5000)
}

public static int httpGet(String url, StringBuilder response, int readTimeout) throws IOException {
    return http(url,(http)->http.setRequestMethod("GET"), response, readTimeout);
}


private static int http(String url, httpMethod method, StringBuilder response) throws IOException {
    return http(url, method, response, 5000);
}

private static int http(String url, httpMethod method, StringBuilder response, int readTimeout) throws IOException {
    HttpURLConnection http = (HttpURLConnection)new URL(url).openConnection();
    http.setConnectTimeout(5000);
    http.setReadTimeout(readTimeout;
    method.doMethod(http);
    int status = 404;
    ......
    ......
    return status;
}

【讨论】:

  • 我认为没有必要创建两个版本的私有http() 方法,对吧?
  • 无需使用未使用的构造函数。我不知道是否还有其他代码依赖于较短的构造函数。
【解决方案2】:

您需要创建 2 个版本的 httpGet 方法(一个带有 readTimeout 参数,另一个没有它,它将使用默认值调用第一个版本):

private static final long DEFAULT_READ_TIMEOUT = 5000;

public static int httpGet(String url, StringBuilder response) throws IOException {
    return httpGet(url, response, DEFAULT_READ_TIMEOUT);
}

public static int httpGet(String url, StringBuilder response, long readTimeout) throws IOException {
    return http(url,(http)->http.setRequestMethod("GET"), response, readTimeout);
}

private static int http(String url, httpMethod method, StringBuilder response, long readTimeout) throws IOException {
    HttpURLConnection http = (HttpURLConnection)new URL(url).openConnection();
    http.setConnectTimeout(5000);
    http.setReadTimeout(readTimeout);
    method.doMethod(http);
    int status = 404;
    ......
    ......
    return status;
}

现在您可以决定提供超时时间

httpGet(DEFAULT_BROWSCAP_ENDPOINT, result, mytimeout)

还是不

httpGet(DEFAULT_BROWSCAP_ENDPOINT, result)

【讨论】:

    【解决方案3】:

    通常这样做的方法是提供许多具有不同签名的不同函数,这些函数只是假设一个默认值。这可以通过将一堆函数“链接”在一起来完成,每个函数都指定它的默认值并调用更详细的函数。例如:

    public void myFunctionWithALotOfArguments(int a, int b, int c, int d) {
        // do stuff
    }
    public void myFunctionWithLessArguments(int a, int b, int c) {
        myFunctionWithALotOfArguments(a, b, c, 50);
    }
    public void myFunctionWithFewArguments(int a, int b) {
        myFunctionWithLessArguments(a, b, 25);
    }
    public void mySimplifiedFunction(int a) {
        myFunctionWithFewArguments(a, 9);
    }
    public void justGuessWhatToDo() {
        mySimplifiedFunction(42);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-17
      • 1970-01-01
      • 2019-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-12
      相关资源
      最近更新 更多