Java教程

Java8 功能接口

仅包含一个抽象方法的接口称为功能接口。它可以具有任意数量的默认静态方法,但只能包含一个抽象方法。它也可以声明对象类的方法。
功能接口也称为单一抽象方法接口或SAM接口。它是Java的一项新功能,有助于实现功能编程方法。

示例1

@FunctionalInterface
interface sayable{
  void say(String msg);
}
public class FunctionalInterfaceExample implements sayable{
  public void say(String msg){
    System.out.println(msg);
  }
  public static void main(String[] args) {
    FunctionalInterfaceExample fie = new FunctionalInterfaceExample();
    fie.say("Hello there");
  }
}
输出:
Hello there

功能接口可以具有对象类的方法。参见以下示例。

示例2

@FunctionalInterface
interface sayable{
  void say(String msg); // abstract method
  // It can contain any number of Object class methods.
  int hashCode();
  String toString();
  boolean equals(Object obj);
}
public class FunctionalInterfaceExample2 implements sayable{
  public void say(String msg){
    System.out.println(msg);
  }
  public static void main(String[] args) {
    FunctionalInterfaceExample2 fie = new FunctionalInterfaceExample2();
    fie.say("Hello there");
  }
}
输出:
Hello there

无效的功能接口

仅当功能接口没有任何抽象方法时,它才能扩展另一个接口。
interface sayable{
  void say(String msg); // abstract method
}
@FunctionalInterface
interface Doable extends sayable{
  // Invalid '@FunctionalInterface' annotation; Doable is not a functional interface
  void doIt();
}
输出:
compile-time error

示例3

在下面的示例中,功能接口扩展为非功能接口。
interface Doable{
  default void doIt(){
    System.out.println("do it now");
  }
}
@FunctionalInterface
interface Sayable extends Doable{
  void say(String msg); // abstract method
}
public class FunctionalInterfaceExample3 implements Sayable{
  public void say(String msg){
    System.out.println(msg);
  }
  public static void main(String[] args) {
    FunctionalInterfaceExample3 fie = new FunctionalInterfaceExample3();
    fie.say("Hello there");
    fie.doIt();
  }
}
输出:
Hello there
do it now

Java预定义功能接口

Java提供了预定义的功能接口,以通过使用lambda和方法引用来处理功能编程。
您还可以定义自己的自定义功能接口。以下是放置在java.util.function包中的功能接口的列表。

接口 说明
BiConsumer <T,U> 它表示一个接受两个输入参数并且不返回结果的操作。
Consumer<T> 它表示一个接受单个参数且不返回结果的操作。
Function<T,R> 它代表一个接受一个参数并返回结果的函数。
Predicate<T> 它表示一个参数的谓词(布尔值函数)。
BiFunction <T,U,R> 它代表一个接受两个参数并返回结果的函数。
BinaryOperator <T> 它表示对两个相同数据类型的操作数的操作。它返回与操作数相同类型的结果。
BiPredicate <T,U> 它代表两个参数的谓词(布尔值函数)。
BooleanSupplier 它表示布尔值结果的提供者。
DoubleBinaryOperator 它表示对两个双精度类型操作数的运算并返回双精度类型值。
DoubleConsumer 它表示一个接受单个double类型参数且不返回结果的操作。
DoubleFunction 它代表一个接受双精度类型参数并产生结果的函数。
DoublePredicate 它表示一个双精度类型实参的谓词(布尔值函数)。
DoubleSupplier 它代表双重结果的供应商。
DoubleToIntFunction 它表示一个接受双精度类型参数并产生int类型结果的函数。
DoubleToLongFunction 它代表一个接受双精度类型参数并产生长型结果的函数。
DoubleUnaryOperator 它表示对单个双精度类型操作数的操作,该操作数会产生双精度类型的结果。
IntBinaryOperator 它表示对两个int类型操作数的运算,并返回int类型结果。
IntConsumer 它表示一个接受单个整数参数且不返回结果的操作。
IntFunction 它代表一个接受整数参数并返回结果的函数。
IntPredicate 它表示一个整数参数的谓词(布尔值函数)。
IntSupplier 它表示整数类型的供应商。
IntToDoubleFunction 它代表一个接受整数参数并返回双精度值的函数。
IntToLongFunction 它表示一个接受整数参数并返回long的函数。
IntUnaryOperator 它表示对单个整数操作数的操作,该操作数产生整数结果。
LongBinaryOperator 它表示对两个长型操作数的运算,并返回长型结果。
LongConsumer 它表示一个接受单个long类型参数并且不返回结果的操作。
LongFunction 它代表一个接受长型参数并返回结果的函数。
LongPredicate 它表示一个长型参数的谓词(布尔值函数)。
LongSupplier 它代表长型结果的供应商。
LongToDoubleFunction 它代表一个接受长型参数并返回双精度类型结果的函数。
LongToIntFunction 它表示一个接受长型参数并返回整数结果的函数。
LongUnaryOperator 它表示对单个long类型操作数的操作,该操作数返回long类型结果。
ObjDoubleConsumer <T> 它表示一个接受对象和双精度参数且不返回结果的操作。
ObjIntConsumer <T> 它表示一个接受对象和整数参数的操作。它不会返回结果。
ObjLongConsumer <T> 它表示一个接受对象和长参数的操作,不返回任何结果。
Supplier<T> 它代表结果的提供者。
ToDoubleBiFunction <T,U> 它代表一个接受两个参数并产生双精度类型结果的函数。
ToDoubleFunction <T> 它代表一个返回双精度类型结果的函数。
ToIntBiFunction <T,U> 它代表一个接受两个参数并返回整数的函数。
ToIntFunction <T> 它代表一个返回整数的函数。
ToLongBiFunction <T,U> 它表示一个接受两个参数并返回long类型结果的函数。
ToLongFunction <T> 它代表一个返回长型结果的函数。
UnaryOperator <T> 它表示对单个操作数的操作,该操作返回与操作数相同类型的结果。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4