Is there an interface similar to Callable but with arguments?

后端 未结 8 1652
小蘑菇
小蘑菇 2021-01-31 02:03

Is there an interface in Java similar to the Callable interface, that can accept an argument to its call method?

Like so:

public interface M         


        
8条回答
  •  终归单人心
    2021-01-31 02:04

    Define an interface like so:

    interface MyFunction {
        O call(I input);
    }
    

    Define a method:

    void getOrderData(final Function func){
        ...
        JSONArray json = getJSONArray();
        if(func!=null) func.call(json);           
    }
    

    Usage example:

    //call getOrderData somewhere in your code
    getOrderData(new Function() {
            @Override
            public Void call(JSONArray input) {
                parseJSON(input);                
                return null;
            }
    });
    

提交回复
热议问题