Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-返回不同的方法_Java - Fatal编程技术网

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);
}