簡體   English   中英

java - 如何從另一個類調用帶有參數的方法?

[英]How to call a method with parameters from another class in 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);
}

您需要一個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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM