美文网首页
JDK8新特性:函数式接口@FunctionalInterfac

JDK8新特性:函数式接口@FunctionalInterfac

作者: wyatt_plus | 来源:发表于2017-12-05 18:06 被阅读0次

    我们常用的一些接口Callable、Runnable、Comparator等在JDK8中都添加了@FunctionalInterface注解

    Callable Runnable
    /**
     * An informative annotation type used to indicate that an interface
     * type declaration is intended to be a <i>functional interface</i> 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
     * <em>not</em> 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.
     *
     * <p>Note that instances of functional interfaces can be created with
     * lambda expressions, method references, or constructor references.
     *
     * <p>If a type is annotated with this annotation type, compilers are
     * required to generate an error message unless:
     *
     * <ul>
     * <li> The type is an interface type and not an annotation type, enum, or class.
     * <li> The annotated type satisfies the requirements of a functional interface.
     * </ul>
     *
     * <p>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,可以知道这个注解有以下特点:

    1、该注解只能标记在"**有且仅有一个抽象方法"的接口上。
    
    2、JDK8接口中的静态方法和默认方法,都不算是抽象方法。
    
    3、接口默认继承java.lang.Object,所以如果接口显示声明覆盖了Object中方法,那么也不算抽象方法。
    
    4、该注解不是必须的,如果一个接口符合"函数式接口"定义,那么加不加该注解都没有影响。加上该注解能够更好地让编译器进行检查。如果编写的不是函数式接口,但是加上了@FunctionInterface,那么编译器会报错。
    

    @FunctionalInterface标记在接口上,“函数式接口”是指仅仅只包含一个抽象方法的接口

    package io.fredia;
    
    @FunctionalInterface
    public interface MyFunctionalInterface {
    
        public void test();
    }
    

    如果一个接口中包含不止一个抽象方法,那么不能使用@FunctionalInterface,编译会报错。

    package io.fredia;
    
    @FunctionalInterface
    public interface MyFunctionalInterface {
    
        public void test();
    
        public void test2();
    }
    

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

    package io.fredia;
    
    @FunctionalInterface
    public interface MyFunctionalInterface {
    
        public void test();
    
        // java.lang.Object中的方法不是抽象方法  
        public boolean equals(Object var1);  
      
        // default不是抽象方法  
        public default void defaultMethod(){ }  
      
        // static不是抽象方法  
        public static void staticMethod(){  }
    }
    
    

    相关文章

      网友评论

          本文标题:JDK8新特性:函数式接口@FunctionalInterfac

          本文链接:https://www.haomeiwen.com/subject/sowcixtx.html