![](/img/trans.png)
[英]How to call a method with parameters as objects from another class 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.