Java8新特性学习第一天

Lambda表达式

一. 基本结构:

(param1, param2, param3) -> {

}

二. 关于函数式接口:

  • 如果一个接口只有一个抽象方法,那么该接口就是一个函数式接口.

  • 如果我们在某个接口上声明了FunctionalInterface注解, 那么编译器就会按照函数式接口的定义来要求该接口.

  • 如果某个接口只有一个抽象方法,但我们并没有给该接口声明FunctionalInterface注解, 那么编译器依旧会将该接口看作是函数式接口.

三. FunctionalInterface Java Doc

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

​ 一种信息注释类型,用于指示接口类型声明是由Java语言规范定义的函数接口

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.

​ 从概念上讲,函数接口只有一个抽象方法。因为 reflect 包中的Method下的默认方法有一个实现,

他不是抽象的。如果一个接口声明一个抽象方法重写了对象的一个公共方法,那么它也不计入接口的抽象方法计数。因

为接口的任何实现都将从{java.Lang.Object }或其他地方实现。

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

​ 注意,可以使用lambda表达式、方法引用或构造函数引用创建函数接口的实例。

If a type is annotated with this annotation type, compilers are required to generate an error message unless:
1.The type is an interface type and not an annotation type, enum, or class
2.The annotated type satisfies the requirements of a functional interface.

​如果一种类型被注释为这种注释类型,编译器需要生成一条错误消息,除非:

  1. 类型是接口类型,而不是注释类型、枚举或类
  2. 带注释的类型满足函数式接口的要求。

猜你喜欢

转载自blog.csdn.net/Jeffery_Ju/article/details/86005953