首页 > 解决方案 > 如何在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);
}

标签: java

解决方案


你要么需要一个类的实例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);
}

推荐阅读