Java Pass Method as Parameter

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

    If you don't need these methods to return something, you could make them return Runnable objects.

    private Runnable methodName (final int arg) {
        return (new Runnable() {
            public void run() {
              // do stuff with arg
            }
        });
    }
    

    Then use it like:

    private void otherMethodName (Runnable arg){
        arg.run();
    }
    

提交回复
热议问题