[英]Optional method parameter in Java
我有這個函數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
實現新的可選參數?
您需要創建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)
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;
}
通常,完成此操作的方式是通過提供許多具有不同簽名(假定默認值)的不同功能。 這可以通過將一系列函數“鏈接”在一起來完成,每個函數都指定其默認值並調用更詳細的函數。 例如:
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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.