Can I pass a Method as parameter of another method in java?

前端 未结 6 479
隐瞒了意图╮
隐瞒了意图╮ 2021-01-04 21:04

I am trying to measure the execution time for several methods. so I was thinking to make a method instead of duplicate same code many times.

Here is my code:

6条回答
  •  悲&欢浪女
    2021-01-04 21:44

    the simplest way to do this is to pass an interface. So something like

    public interface CommonFuncs {
    
       public void execute();
       ... more functions
    
    }
    

    and then in you can have methods that take CommonFuncs as an argument. The implementations of those methods can call the execute method.

提交回复
热议问题