什么是函数式接口 @FunctionalInterface

源码定义

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

翻译

  1. 如果一个接口只有一个抽象方法,那么该接口就是函数式接口。
  2. 如果一个接口里面有一个抽象方法,该方法重写了Object的方法,那么不会向接口的抽象方法加1,该接口的抽象方法还是一个,仍然满足函数式接口的定义,那么该接口也会定义为函数式接口。
  3. 如果我们在某个接口上声明了FunctionalInterface注解,那么编译器就会按照函数式接口的定义来要求该接口,不满足就会报错。
  4. 如果某一个接口只有一个抽象方法,但是我们并没有在该接口上声明FunctionalInterface注解,那么编译器依旧会将该接口看作函数式接口。

示例

正确定义

  • 一般定义
package com.demo.fuctionalface;

@FunctionalInterface
public interface InterfaceDemo {
void say();
}
  • 不添加@FunctionalInterface注解编译器也会将此接口当成函数式接口,建议添加
package com.demo.fuctionalface;

public interface InterfaceDemo {
void say();
}
  • 重写了Object中的方法,仍然是函数式接口
package com.demo.fuctionalface;

public interface InterfaceDemo {
void say();
@Override
String toString();
}

重点解释一下为什么第三种也是函数式接口:

  • 首先 Object类是所有java的父类,所以InterfaceDemo的实现类,直接或者间接的继承Object类,也就是说也继承了Object的toString方法,实现在Object内。只需要实现say()方法即可。子类只需要实现一个抽象方法,符合函数式接口的定义。

错误定义

  • 编译器就会报错
package com.demo.fuctionalface;

@FunctionalInterface
public interface InterfaceDemo {
void say();
String MyString();
}

java8新特性 - 什么是函数式接口 @FunctionalInterface?的更多相关文章

  1. java8新特性学习:函数式接口

    本文概要 什么是函数式接口? 如何定义函数式接口? 常用的函数式接口 函数式接口语法注意事项 总结 1. 什么是函数式接口? 函数式接口其实本质上还是一个接口,但是它是一种特殊的接口:SAM类型的接口 ...

  2. Java8新特性探索之函数式接口

    一.为什么引入函数式接口 作为Java函数式编程爱好者,我们都知道方法引用和 Lambda 表达式都必须被赋值,同时赋值需要类型信息才能使编译器保证类型的正确性. 我们先看一个Lambda代码示例: ...

  3. Java JDK1.8新特性之四大函数式接口

    JDK 1.8的一些新特性 四大核心函数式接口(Consumer.Predicate.Supplier.Function),结合lambda表达式 import java.util.ArrayList ...

  4. JAVA 8 主要新特性 ----------------(四)Lambda函数式接口

    一.什么是函数式接口 只包含一个抽象方法的接口,称为函数式接口.  你可以通过 Lambda 表达式来创建该接口的对象.(若 Lambda 表达式抛出一个受检异常,那么该异常需要在目标接口的抽象方法 ...

  5. 乐字节-Java8核心特性实战之函数式接口

    什么时候可以使用Lambda?通常Lambda表达式是用在函数式接口上使用的.从Java8开始引入了函数式接口,其说明比较简单:函数式接口(Functional Interface)就是一个有且仅有一 ...

  6. Java8新特性第2章(接口默认方法)

    在Java中一个接口一旦发布就已经被定型,除非我们能够一次性的更新所有该接口的实现,否者在接口的添加新方法将会破坏现有接口的实现.默认方法就是为了解决这一问题的,这样接口在发布之后依然能够继续演化. ...

  7. 01 语言基础+高级:1-10 JDK8新特性_day12【函数式接口】

    day12[函数式接口] 主要内容自定义函数式接口函数式编程常用函数式接口 教学目标能够使用@FunctionalInterface注解能够自定义无参无返回函数式接口能够自定义有参有返回函数式接口能够 ...

  8. Java8新特性探索之Stream接口

    一.为什么引入Stream流 流是一系列与特定存储机制无关的元素--实际上,流并没有"存储"之说.使用流,无需迭代集合中的元素,就可以从管道提取和操作元素.这些管道通常被组合在一起 ...

  9. java8新特性- 默认方法 在接口中有具体的实现

    案例分析 在java8中在对list循环的时候,我们可以使用forEach这个方法对list进行遍历,具体代码如下demo所示 public static void main(String[] arg ...

随机推荐

  1. django_4数据库3——admin

    生成admin界面 1.setting.py中,保证'django.contrib.admin',应用打开,django1.11默认打开的 2.url.py中的admin默认时打开的 3.对model ...

  2. linux磁盘分区、格式化、挂载

    新建分区的操作步骤,如下图: 1)RAID卡: 机器有没有RAID卡可以在开机时看有没有出现配置RAID什么的提示(亲测),系统运行时有没有,不知道! 服务器大多有这个新加硬盘后不修改raid,开即f ...

  3. 0MQ是会阻塞的,不要字面上看到队列就等同非阻塞。

    如果你是希望通过0MQ来做缓冲队列,非阻塞的效果,那你就必须清楚 0MQ Socket是会阻塞,你要搞清楚0MQ Socket与队列的关系. 官方协议文档规定了,一部分类型的 0MQ Socket为不 ...

  4. 2018092609-2 选题 Scrum立会报告+燃尽图 01

    此作业要求参见:[https://edu.cnblogs.com/campus/nenu/2019fall/homework/8683] 一.小组情况组长:迟俊文组员:宋晓丽 梁梦瑶 韩昊 刘信鹏队名 ...

  5. Linux\CentOS MySql 安装与配置

    一.MySQL 简介 MySQL 是一个关系型数据库管理系统,是MySQL AB公司开发,现在属于 Oracle 旗下产品. MySQL 采用标准化语言.体积小.速度快.成本低.开源等特点使得一些中小 ...

  6. GitHub 标星 1.6w+,我发现了一个宝藏项目,作为编程新手有福了!

    大家好,我是 Rocky0429,一个最近老在 GitHub 上闲逛的蒟蒻... 特别惭愧的是,虽然我很早就知道 GitHub,但是学会逛 GitHub 的时间特别晚.当时一方面是因为菜,看着这种全是 ...

  7. Gemini.Workflow 双子工作流高级教程:数据库-设计文档

    数据库设计文档 数据库名:Workflow_New 序号 表名 说明 1 WF_Activity wf_Activity 2 WF_ActivityInstance wf_ActivityInstan ...

  8. 【Luogu P1972】HH的项链

    Luogu P1972 一开始非常naive随便打了个树状数组统计就交上去了,然后不出意料的爆零了-- 然后删一删改一改过了. 重点:对于区间[1,r]中重复出现的数,我们只需要关心最右边那一个是否在 ...

  9. ctf中关于图片的隐写随笔(不全)

    ①JPG图片的结束符,十六进制常为FFD9 ②binwalk的原理是:检查常见的文件头信息,如果不符合,一定有隐藏信息.③JPG是有损压缩,PNG是无损压缩,BMP是不压缩. 隐写的基本原理:图片查看 ...

  10. 多进程使用同一log4j配置导致的日志丢失与覆盖问题

    最近接手了一个流传很多手的魔性古早代码,追日志时发现有明显缺失.对log4j不熟,不过可以猜测日志出问题肯定和多进程使用同一个log4j配置有关.经多次排查,终于捋清了其中逻辑.本文对排查过程进行复盘 ...