Java Pass Method as Parameter

后端 未结 16 1029
滥情空心
滥情空心 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:59

    Java do have a mechanism to pass name and call it. It is part of the reflection mechanism. Your function should take additional parameter of class Method.

    public void YouMethod(..... Method methodToCall, Object objWithAllMethodsToBeCalled)
    {
    ...
    Object retobj = methodToCall.invoke(objWithAllMethodsToBeCalled, arglist);
    ...
    }
    

提交回复
热议问题