JDK8 FunctionalInterface

/**

  • An informative annotation type used to indicate that an interface
  • type declaration is intended to be a functional interface as
  • defined by the Java Language Specification.
  • FunctionalInterface 是一个标记接口。
  • Conceptually, a functional interface has exactly one abstract
  • method. Since {@linkplain java.lang.reflect.Method#isDefault()
  • default methods} have an implementation, they are not abstract.
    在概念上一个funcitonalInterface 只有一个抽象方法。可以通过反射的Method#isDefault() 检测。
    If an interface declares an abstract method overriding one of the
  • public methods of {@code java.lang.Object}, that also does
  • not count toward the interface’s abstract method count
  • since any implementation of the interface will have an
  • implementation from {@code java.lang.Object} or elsewhere.
  • 如果接口继承并是实现了Object的方法,那么不会作为抽象方法的统计范围。
  • Note that instances of functional interfaces can be created with

  • lambda expressions, method references, or constructor references.
  • functionalInterface 一般用作lambda表达式,方法引用,构造方法引用
  • If a type is annotated with this annotation type, compilers are

  • required to generate an error message unless:
  • 被标记了functionalInterface的接口,会在编译的时候做检测
    • The type is an interface type and not an annotation type, enum, or class.
    • 必须时接口
    • The annotated type satisfies the requirements of a functional interface.
    • 统计之后之后,直有一个功能方法,也就是直有一个抽象方法
    • However, the compiler will treat any interface meeting the

    • definition of a functional interface as a functional interface
    • regardless of whether or not a {@code FunctionalInterface}
    • annotation is present on the interface declaration.
    • 然而,编译器会将只有一个抽象方法的接口,自动认为时functional interface
    • @jls 4.3.2. The Class Object
    • @jls 9.8 Functional Interfaces
    • @jls 9.4.3 Interface Method Body
    • @since 1.8
      */
      @Documented
      @Retention(RetentionPolicy.RUNTIME)
      @Target(ElementType.TYPE)
      public @interface FunctionalInterface {}
    • 总结一下:
      functional interface 是一个非必须的编译时期的标记接口。
      实现方式:
      通过统计除default 实现, static 实现, object基础方法以外的抽象方法数量。 如果统计结果直有一个抽象方法,那么编译通过。 否则编译出错。

      也就是说该功能,在编译期间就生效的注解。

    • 0
      点赞
    • 0
      收藏
      觉得还不错? 一键收藏
    • 0
      评论
    JDK 8 中引入的函数式编程特性主要包括函数接口(Functional Interface)和Lambda 表达式。函数接口是指仅包含一个抽象方法的接口,用于表示可以作为函数对象的接口。Lambda 表达式是一种简洁的语法,用于创建函数对象。 底层原理如下: 1. 函数接口:函数接口是 JDK 8 中引入的一个新的注解 @FunctionalInterface 标识的接口。该接口只能包含一个抽象方法,但可以包含多个默认方法和静态方法。通过函数接口,我们可以将方法作为参数传递、作为返回值返回,以及在 Lambda 表达式中使用。 2. Lambda 表达式:Lambda 表达式是一种匿名函数,它可以简洁地表示带有参数和代码块的函数对象。Lambda 表达式的语法由参数列表、箭头符号 "->" 和方法体组成。例如 `(x, y) -> x + y` 表示一个接收两个参数并返回它们之和的 Lambda 表达式。 3. 函数对象:Lambda 表达式实际上是函数对象的一种实现方式。在编译时,Lambda 表达式会被编译成一个函数对象,该对象实现了函数接口中的抽象方法。当我们使用 Lambda 表达式时,实际上是创建了一个函数对象,并将其作为方法参数或返回值进行传递。 通过函数接口和Lambda 表达式,我们可以将函数作为一等公民进行处理,实现了函数式编程的特性,如高阶函数、函数组合、柯里化等。底层原理是将Lambda 表达式编译成函数对象,从而实现了函数式编程的功能。这样可以提高代码的可读性和简洁性,并且使得并行处理和函数式操作更加方便。

    “相关推荐”对你有帮助么?

    • 非常没帮助
    • 没帮助
    • 一般
    • 有帮助
    • 非常有帮助
    提交
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值