Help with understanding a function object or functor in Java

后端 未结 4 665
甜味超标
甜味超标 2020-12-01 00:28

Can someone explain what a functor is and provide a simple example?

4条回答
  •  旧时难觅i
    2020-12-01 00:43

    A functor is an object that's a function.

    Java doesn't have them, because functions aren't first-class objects in Java.

    But you can approximate them with interfaces, something like a Command object:

    public interface Command {
        void execute(Object [] parameters); 
    }
    

    Updated on 18-Mar-2017:

    Since I first wrote this JDK 8 has added lambdas. The java.util.function package has several useful interfaces.

提交回复
热议问题