如何用Java中另一个类的参数调用方法?

时间:2019-02-12 22:52:40

标签: java

我遇到了一个问题,我想从一个类到另一个类调用一个方法,但是该方法有一个参数。我尝试从另一堂课上做String searchData = "searchValue",但未调用另一堂课。

这是我的代码:

我要调用的方法

public List<JobSearchItem> getJobAutocomplete(String searchValue) {
    String sValue = "%" + searchValue.toUpperCase() + "%";
    return dataUtilityService.getJdbcTemplate().query(SQL_GET_JOB_SEARCH_LISTS, 
                                new Object[]{sValue, sValue}, jobSearchItemMapper);
} 

我要调用上面代码的另一种方法

public void loadSearchList() throws SQLException, NamingException, URISyntaxException, IOException, ParseException {

   String searchData = "searchValue";
  List<JobSearchItem> jobSearchList = XPayService.getJobAutocomplete(searchData);


    this.setJobSearchItems(jobSearchList);
}

1 个答案:

答案 0 :(得分:1)

您或者需要一个XPayService类的实例来调用该方法,否则可以使该方法成为静态方法

使用该类的实例:

class XPayService() {
    public List<JobSearchItem> getJobAutocomplete(String searchValue) {
        String sValue = "%" + searchValue.toUpperCase() + "%";
        return dataUtilityService.getJdbcTemplate().query(SQL_GET_JOB_SEARCH_LISTS, 
                                    new Object[]{sValue, sValue}, jobSearchItemMapper);
    }
}

public void loadSearchList() throws SQLException, NamingException, URISyntaxException, IOException, ParseException {
    XPayService xps = new XPayService();
    String searchData = "searchValue";
    List<JobSearchItem> jobSearchList = xps.getJobAutocomplete(searchData);
    this.setJobSearchItems(jobSearchList);
}

使用静态方法:

class XPayService() {
    public static List<JobSearchItem> getJobAutocomplete(String searchValue) {
        String sValue = "%" + searchValue.toUpperCase() + "%";
        return dataUtilityService.getJdbcTemplate().query(SQL_GET_JOB_SEARCH_LISTS, 
                                    new Object[]{sValue, sValue}, jobSearchItemMapper);
    }
}

public void loadSearchList() throws SQLException, NamingException, URISyntaxException, IOException, ParseException {
    String searchData = "searchValue";
    List<JobSearchItem> jobSearchList = XPayService.getJobAutocomplete(searchData);
    this.setJobSearchItems(jobSearchList);
}