在这篇文章中,咱们将了解到在Java8下如何进行函数式编程。java
所谓的函数式编程就是把函数名字当作值进行传递,而后接收方拿到这个函数名进行调用。编程
首先来看下JavaScript如何进行函数调用ide
var Person = { sayHello: function(name) { alert('hello ' + name); } }; function work(fn) { fn('Jim'); } work(Person.sayHello); // hello Jim
在这个例子中,sayHello
函数被当作一个参数传递到另外一个work
函数当中,而后work调用给定的函数。函数式编程
在Java中,充当函数的角色是类中方法,在本篇文章当中提到的函数泛指方法。函数
接下来看下Java8中一个简单的函数式编程例子:设计
import java.util.function.Consumer; class Person { public static void sayHello(String name) { System.out.println("hello " + name); } } public class TestPerson { public static void main(String[] args) { work(Person::sayHello); // hello Jim } public static void work(Consumer<String> consumer) { consumer.accept("Jim"); } }
从这个例子中能够看到,Java8传递函数的方式跟JavaScript不同,在Java8当中传递函数的方式是:方法引用::方法名称
,如:String.valueOf,String.toString。接收参数Consumer能够先不用去了解,后面会讲到。code
为了接收传递过来的函数引用,Java设计者提出了一个函数式接口
的概念接口
函数式接口定义:一个接口里面只有一个抽象方法
。ip
以下面几个接口都是函数式接口:源码
查看Runnable类的源码能够发现,在类上方定义了一个@FunctionalInterface
注解
@FunctionalInterface public interface Runnable { public abstract void run(); }
当作一个标记使用,标记当前接口是一个函数式接口,若是你的接口有多个抽象方法或没有抽象方法,那么会编译报错。它的做用有点相似于@override
注解,有辅助做用,但不是必须的。若是你想设计一个函数式接口,那么最好把它加上。
// 编译不经过,必需要有一个抽象方法 @FunctionalInterface public interface InterfaceA { } // 编译经过,只有一个抽象方法 @FunctionalInterface public interface InterfaceB { void run(); } // 编译不经过,只能有一个抽象方法 @FunctionalInterface public interface InterfaceC { void run(); void run2(); } // 编译经过,run2是默认方法,并非抽象方法 // 在这里只有一个run抽象方法 @FunctionalInterface public interface InterfaceD { void run(); default void run2() {}; }
在例子InterfaceD中涉及到了接口默认方法default void run2() {};
,这里略过不讲,后续文章中会有提到。
咱们能够实现一个自定义的函数式接口,替代Consumer
// 自定义函数式接口 @FunctionalInterface public interface FunctionCaller<T> { void call(T t); } public class TestPerson2 { public static void main(String[] args) { work(Person::sayHello); } public static void work(FunctionCaller<String> caller) { caller.call("Jim"); } }
本篇主要讲解了在Java8下如何进行函数式编程 ,以及如何定义和使用一个函数式接口。