简体   繁体   English

java - 如何从另一个类调用带有参数的方法?

[英]How to call a method with parameters from another class in java?

I am coming to a problem where I want to call a method from one class to another, but this method has a parameters.我遇到了一个问题,我想从一个类调用一个方法到另一个类,但是这个方法有一个参数。 I tried doing String searchData = "searchValue" from my another class, but it for my other class is not being called.我尝试从我的另一个班级中执行String searchData = "searchValue" ,但没有为我的另一个班级调用它。

Here is my code:这是我的代码:

The method that I want to call我想调用的方法

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

the other method that I want to call the above code我想调用上面代码的另一种方法

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

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


    this.setJobSearchItems(jobSearchList);
}

You either need an instance of the XPayService class to call the method on, or else you can make the method static您需要一个XPayService类的实例来调用该方法,否则您可以将该方法设为静态

Using an instance of the class:使用类的实例:

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

Using a static method:使用静态方法:

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