Java-返回不同的方法
我有一个抽象类,它有一个从站点加载数据的方法,并返回一个处理接收到的数据的方法Java-返回不同的方法,java,Java,我有一个抽象类,它有一个从站点加载数据的方法,并返回一个处理接收到的数据的方法 protected final FormatRate load(String urlToSite, SiteLoader.Currency currencyName){ StringBuilder content; boolean error; int retryCount = 0; do{ content = new StringBuilder();
protected final FormatRate load(String urlToSite, SiteLoader.Currency currencyName){
StringBuilder content;
boolean error;
int retryCount = 0;
do{
content = new StringBuilder();
error = false;
try {
// create a url object
HttpURLConnection con = (HttpURLConnection) new URL(urlToSite).openConnection();
con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0");
con.setConnectTimeout(50000); //set timeout to 50 seconds
con.setReadTimeout(50000); //set timeout to 50 seconds
try(BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()))){
String line;
while ((line = bufferedReader.readLine()) != null)
{
content.append(line).append("\n");
}
}
}
catch(Exception e)
{
error = true;
retryCount++;
System.err.println("" + e.getMessage());
}
} while (error && retryCount < 5);
if(error){
throw new RuntimeException("");
}
return handle(content.toString(), currencyName);
}
及其实施:
@Override
protected FormatRate handle(String content, Currency currencyName) {
int indexRate = content.indexOf(strForRate);
int indexDate = content.indexOf(strForDate);
int indexScale = content.indexOf(strForScale);
int indexAbbreviation = content.indexOf(strForAbbreviation);
String scale = "";
if (currencyName.getId().equals(Currency.RUB.getId())) {
scale = content.substring(indexScale + 11, indexScale + 14);
} else {
scale = content.substring(indexScale + 11, indexScale + 12);
}
return new FormatRate(content.substring(indexRate + 18, indexRate + 24),
scale,
content.substring(indexAbbreviation + 19, indexAbbreviation + 22),
content.substring(indexDate + 7, indexDate + 26));
}
我的问题是:我需要添加一个新的方法handle
,它将返回一个集合。当调用具有三个参数的方法load
时,将调用它。我不明白如何实施这个
方法load
也在类中声明为抽象
@Override
public FormatRate load(Currency currencyName) {
return load("https://www.nbrb.by/api/exrates/rates/" + currencyName.getId(), currencyName);
}
@Override
public FormatRate load(Currency currencyName, String date) {
return load("https://www.nbrb.by/api/exrates/rates/" + currencyName.getId() + "?ondate=" + date, currencyName);
}
@Override
public FormatRate load(Currency currencyName, String dateFrom, String dateTo) {
return load("https://www.nbrb.by/api/exrates/rates/dynamics/" + currencyName.getId() +
"?startDate=" + dateFrom + "&endDate=" + dateTo, currencyName);
}
您需要重载方法
句柄并更改方法返回的值的类型吗?重载方法需要返回集合
,而不是格式化率
?@Abra是的,没错。该方法应返回一个集
。在方法load
中,调用我需要的方法handle
,您不能重载具有不同返回类型的方法。您只能重载具有不同参数数或不同参数类型的方法。@Abra我知道我需要更改重载方法的参数,但我不明白如何返回一个“handle”方法。
@Override
public FormatRate load(Currency currencyName) {
return load("https://www.nbrb.by/api/exrates/rates/" + currencyName.getId(), currencyName);
}
@Override
public FormatRate load(Currency currencyName, String date) {
return load("https://www.nbrb.by/api/exrates/rates/" + currencyName.getId() + "?ondate=" + date, currencyName);
}
@Override
public FormatRate load(Currency currencyName, String dateFrom, String dateTo) {
return load("https://www.nbrb.by/api/exrates/rates/dynamics/" + currencyName.getId() +
"?startDate=" + dateFrom + "&endDate=" + dateTo, currencyName);
}