java8 functionalinterface注解_JDK8 新特性 @FunctionalInterface 函数式接口

在 JDK8 中,我们常用的一些接口 Callable、Runnable、Comparator 等都添加了 @FunctionalInterface 注解,那这个注解是什么意思呢?

什么是函数式接口

源码

package java.lang;

import java.lang.annotation.*;

/**

* 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.

*

* 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. 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.

*

*

Note that instances of functional interfaces can be created with

* lambda expressions, method references, or constructor references.

*

*

If a type is annotated with this annotation type, compilers are

* required to generate an error message unless:

*

*

*

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.

*

* @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 {}

复制代码

解释说明

通过 JDK8 源码 javadoc,可以知道这个注解有以下特点

该注解只能标记在 "有且仅有一个抽象方法" 的接口上

JDK8 接口中的静态方法和默认方法,都不算是抽象方法

由于所有接口默认继承 java.lang.Object,所以如果接口显示声明覆盖了 Object 中方法,那么也不算抽象方法

该注解不是必须的,如果一个接口符合 "函数式接口" 定义,那么加不加该注解都没有影响。加上该注解能够更好地让编译器进行检查。如果编写的不是函数式接口,但是加上了 @FunctionInterface,那么编译器会报错。

下面这个接口就是一个正确的函数式接口:

@FunctionalInterface

public interface TestFunctionalInterface{

// 抽象方法

public void sub();

// java.lang.Object中的方法不是抽象方法

public boolean equals(Object var1);

// default不是抽象方法

public default void defaultMethod(){

}

// static不是抽象方法

public static void staticMethod(){

}

}

复制代码

函数式接口的作用

首先我们定义一个普通接口和一个函数式接口

5c9060d79a7bd93ddcf2ab177e1515b9.png

c5bde8e5b620a427203a0478a7531294.png

来看下将这个两个接口作为参数时,传参写法有什么不同

f76699f91c26623902ccecc595a20f6a.png

如上图可以看到,传入函数式接口的实例时,由于只有一个接口, ide 提示我们可以写成红框中的形式,是不是特别像是传入了一个函数(实际上传入的是一个接口的实现),这就是它为什么叫函数式接口的原因

总结:函数式接口的作用只是让接口的实现写起来像一个函数,以配合java 8 中的函数式编程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值