设为首页 加入收藏

TOP

JAVA函数式编程(二)
2023-08-06 07:49:34 】 浏览:89
Tags:JAVA
话那么就只能有一个抽象方法接口,目的是为了保证接口的设计人员在接口中添加多个抽象方法时能够在编译时得到错误提示。例如一个接口标记了 @FunctionalInterface 注解若它包含多个抽象方法,编译器会报错。一般函数式接口只能有一个抽象方法,并且排除接口默认(default)方法及声明中覆盖Object的公开方法,同时,@FunctionalInterface不能标注在注解、类、枚举上。如果违背以上的原则那么这个接口就不能视为函数式接口,当标注这个注解后编译会报错, 不过任何一个接口满足上述函数式接口的要求后,无论接口上是否添加@FunctionallInterface注解都能被编译器视为函数式接口。无论是哪一种注解其实都是数据流转对象。

理解@FunctionallInterface

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 default methods have an implementation, they are not abstract. If an interface declares an abstract method overriding one of the public methods of 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 java.lang.Object or elsewhere.

一种信息类的注解类型,用于接口类型声明,旨在成为 Java 语言规范定义的功能性接口。从概念上讲,函数接口只能有一个抽象方法。但由于默认方法已经被实现,所以它们不是抽象的。如果接口声明了一个覆盖java.lang.Object公共方法之一的抽象方法,则该方法也不计入接口的抽象方法计数,因为该接口的任何实现都将具有来自java.lang.Object或其他地方的实现。

例如:

@FunctionalInterface
public interface C301_FunctionalInterface {
    /**
     * 重写的方法不算,因为会默认调用其继承父类的这个方法
     */
    @Override
    String toString();

    /**
     * default方法由于会在定义时实现,所以不作为函数式接口唯一的方法的要求
     *
     * @return String
     */
    default String toUpperCase() {
        return "toUpperCase";
    }

    void test();

    public static void main(String[] args) {
        /**调用之前需要先实现其定义的方法  test(),这个地方也就体现出为啥只能定义一个抽象方法,否则该如何表示实现哪个默认方法呢?*/
        C301_FunctionalInterface functionalInterface = () -> {
            System.out.println("test");
        };
        /**调用了其定位的方法*/
        functionalInterface.test();
        System.out.println(functionalInterface.toUpperCase());
        System.out.println(functionalInterface.toString());
    }
}

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 FunctionalInterface annotation is present on the interface declaration.

请注意,可以使用 lambda 表达式、方法引用或构造函数引用创建函数接口的实例。此类型可以作用的对象是该类型是接口,而不能注解、枚举,否则编译器会生成错误消息 ,带注解的类型满足功能接口的要求。但是,无论是否使用这个注解,只需要接口满足函数式接口的要求都可以被编译器视为函数式接口。

理解Supplier<T>

位于java.util.function.Supplier<T>,它在 Java 8 中引入。该接口代表一个供给型接口,它不接受任何参数,返回一个类型为 T 的结果。该接口有一个抽象方法 T get(),用于获取结果,数据提供类型, 特点是只出不进,一般作为方法、构造参数,方法返回值。

/**
 * Represents a supplier of results.
 *
 * <p>There is no requirement that a new or distinct result be returned each
 * time the supplier is invoked.
 *
 * <p>This is a <a href="package-summary.html">functional interface</a>
 * whose functional method is {@link #get()}.
 *
 * @param <T> the type of results supplied by this supplier
 *
 * @since 1.8
 */
@FunctionalInterface
public interface Supplier<T> {

    /**
     * Gets a result.
     *
     * @return a result
     */
    T get();
}

示例使用 Supplier 接口:

import java.util.function.Supplier;

public class Suppli
首页 上一页 1 2 3 4 5 6 下一页 尾页 2/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇@ControllerAdvice 注解使用及原.. 下一篇《深入理解Java虚拟机》读书笔记..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目