Java 中的可选方法参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32244155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Optional method parameter in Java
提问by Shaonline
I have this function httpGet()
which calls http()
:
我有这个函数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;
}
I want to add an additional parameter for readTimeout
, which needs to be optional with a default value that will be used otherwise.
我想添加一个额外的参数 for readTimeout
,它需要是可选的,默认值将在其他情况下使用。
In this case readTimeout
is set to 5000 for all the calls, but I want this specific call to be executed for longer timeouts.
在这种情况下readTimeout
,所有调用都设置为 5000,但我希望在更长的超时时间内执行此特定调用。
I think I need this new parameter to be optional as I don't want to change the implementations where this http()
method has been called.
我想我需要这个新参数是可选的,因为我不想更改http()
调用此方法的实现。
This is how I call it:
我是这样称呼它的:
Assert.assertEquals(HTTP_OK, httpGet(DEFAULT_BROWSCAP_ENDPOINT, result));
How can I implement a new optional parameter for readTimeout
?
如何为 实现新的可选参数readTimeout
?
采纳答案by IceArdor
Java doesn't have default values like Python, C++, VBA, Delphi, and many languages. Create new constructors with the alternate signature.
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;
}
回答by sdabet
You need to create 2 versions of the httpGet
method (one with the readTimeout
parameter, and another one without it which will call the first version with a default value):
您需要创建该httpGet
方法的2 个版本(一个带有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;
}
Now you can decide to provide the timeout
现在您可以决定提供超时
httpGet(DEFAULT_BROWSCAP_ENDPOINT, result, mytimeout)
or not
或不
httpGet(DEFAULT_BROWSCAP_ENDPOINT, result)
回答by Parker Hoyes
Generally the way this is done is by providing many different functions with different signatures that just assume a default value. This can be done by "chaining" a bunch of functions together, each specifying it's default value and calling the more detailed function. For example:
通常,这样做的方法是提供许多具有不同签名的不同函数,这些函数仅采用默认值。这可以通过将一堆函数“链接”在一起来完成,每个函数指定它的默认值并调用更详细的函数。例如:
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);
}