Java8 function interface @FunctionalInterface

FunctionalInterface comments

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

Annotation information for indicating the type of interface, the interface type declaration is a function
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.

Conceptually, the function interface only an abstract method. Since {@linkplain java.lang.reflect.Method # isDefault ()} has a default method implemented, they are not abstract. If an interface declares an abstract method to cover one of the
common methods of {@code java.lang. Object}, which is
not counted as an abstract method of counting any implementation interface will have an interface
implemented from {@code java.lang. Object} or elsewhere.

Note that instances of functional interfaces can be created with
 * lambda expressions, method references, or constructor references.

Examples of functional interface through lamda expression, function reference constructor to create

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

If a statement of this type add a comment, unless the interface of two things will complain to satisfy
1, this type of comment is the interface rather than the type of enumeration type or class
2, this type of statement in line with the requirements of function interfaces

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

However, the interface compiler will comply with those functions are defined as a function interface, whether the interface has @FunctionalInterface comments

What is the function interface

The so-called function interface, an interface is of course first of all, then there is at this interface can have only an abstract method.

This type of interface is also called SAM interfaces, i.e. Single Abstract Method interfaces

Feature

Interface and only an abstract method
allows you to define a static method
allows to define the default method
public method in java.lang.Object allows
the annotation is not required, if an interface matching "function interface" is defined, then did to all the notes No effect. Plus the notes to better allow the compiler to be checked. If writing is not functional interface, but added @FunctionInterface, then the compiler will complain

to sum up

1, if an interface has an abstract method, then the interface is a function of interface
2, if a declaration of the interface @FunctionalInterface, as defined by the compiler will be required interface function of the interface
3, if only a certain interface abstract methods, we have not a single statement FunctionalInterface notes to the interface, the compiler will still be seen as a function of the Interface interface

Examples of functional interface through lamda expression, function reference constructor to create

example

// 正确的函数式接口
@FunctionalInterface
public interface TestInterface {
 
    // 抽象方法
    public void sub();
 
    // java.lang.Object中的public方法
    public boolean equals(Object var1);
 
    // 默认方法
    public default void defaultMethod(){
    
    }
 
    // 静态方法
    public static void staticMethod(){
 
    }
}

// 错误的函数式接口(有多个抽象方法)
@FunctionalInterface
public interface TestInterface2 {

    void add();
    
    void sub();
}

A simple example of
definition of a Hello interface:

@FunctionalInterface
public interface Hello {
    String msg(String info);
}

transfer

Hello hello = param -> param + "world!";
System.out.println("test functional:" + hello.msg("hello,"));

Export

test functional:hello,world!

JDK function interface in Examples

java.lang.Runnable,

java.awt.event.ActionListener,

java.util.Comparator,

java.util.concurrent.Callable

java.util.function interface in packages, such as Consumer, Function, Predicate, Supplier like
reference Java 8 FunctionalInterface depth analysis of (a)

Functional Interface @FunctionalInterface

Published 80 original articles · won praise 140 · views 640 000 +

Guess you like

Origin blog.csdn.net/linjpg/article/details/104096062