Java Pass Method as Parameter

后端 未结 16 1019
滥情空心
滥情空心 2020-11-22 02:17

I am looking for a way to pass a method by reference. I understand that Java does not pass methods as parameters, however, I would like to get an alternative.

I\'ve

16条回答
  •  难免孤独
    2020-11-22 02:46

    I'm not a java expert but I solve your problem like this:

    @FunctionalInterface
    public interface AutoCompleteCallable {
      String call(T model) throws Exception;
    }
    

    I define the parameter in my special Interface

    public  void initialize(List entries, AutoCompleteCallable getSearchText) {.......
    //call here
    String value = getSearchText.call(item);
    ...
    }
    

    Finally, I implement getSearchText method while calling initialize method.

    initialize(getMessageContactModelList(), new AutoCompleteCallable() {
              @Override
              public String call(Object model) throws Exception {
                return "custom string" + ((xxxModel)model.getTitle());
              }
            })
    

提交回复
热议问题