package java.lang;

import java.lang.annotation.*;

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

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.

打捞要点之后有这么三点:

1. a functional interface has exactly one abstract method.

要声明FunctionalInterface的接口只能有1个抽象方法;

2. Since default methods have an implementation, they are not abstract.

因为default方法有实现,所以它也不是抽象方法, 所以不在列;

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

因为这里限定抽象方法只能有1个,那么如果我的接口里声明了一个 抽象方法"String toString();"或者"boolean equals(Object obj);"这样的,算不算呢? --->答案是: 不算这个,因为任何实现接口的对象都会继承Object,所以这些public的Object的方法,不算在列的。

也就是说,你的接口可以声名如下:

/**
* Created by niewj on 2017/10/26.
*/
@FunctionalInterface
public interface FunctionalInterfaceSummary { int doSum(int x, int y); // 抽象方法 String toString(); // 不占1的数额 boolean equals(Object obj); // 不占1的数额
// boolean equals(); // 这个会占1的数额, 因为这不是Object中的方法 default void display() {
System.out.println("show sth..");
}
}

小结:

1. 如果一个接口里只限定一个抽象方法,则可以用注解@FunctionalInterface 限定;

2. 声明为函数式接口之后,就有以下特征:

a. 只有一个抽象方法;

b.  Object类中的public的方法也在接口里写出来的, 也可以,因为这写不算;

c.  default方法不算, 因为它们是implementation。不是abstract!

阅源-jdk8-FunctionalInterface注解的更多相关文章

  1. 关于JAVA中源码级注解的编写及使用

    一.注解简介: 1.1.什么是"注解": ​ 在我们编写代码时,一定看到过这样的代码: class Student { private String name; @Override ...

  2. Java @FunctionalInterface注解-6

    在学习 Lambda 表达式时,我们提到如果接口中只有一个抽象方法(可以包含多个默认方法或多个 static 方法),那么该接口就是函数式接口.@FunctionalInterface 就是用来指定某 ...

  3. Spring源码系列 — 注解原理

    前言 前文中主要介绍了Spring中处理BeanDefinition的扩展点,其中着重介绍BeanDefinitionParser方式的扩展.本篇文章承接该内容,详解Spring中如何利用BeanDe ...

  4. Spring源码之注解扫描Component-scan

    本文主要介绍Spring的component-scan标签,了解spring是如何实现扫描注解进行bean的注册,主要实现实在 NamespaceHandler, NamespaceHandlerSu ...

  5. Spring源码之注解的原理

    https://blog.csdn.net/qq_28802119/article/details/83573950 https://www.zhihu.com/question/318439660/ ...

  6. JDK8新特性:函数式接口@FunctionalInterface的使用说明

    我们常用的一些接口Callable.Runnable.Comparator等在JDK8中都添加了@FunctionalInterface注解. 通过JDK8源码javadoc,可以知道这个注解有以下特 ...

  7. JDK8 新特性

    JDK8 新特性目录导航: Lambda 表达式 函数式接口 方法引用.构造器引用和数组引用 接口支持默认方法和静态方法 Stream API 增强类型推断 新的日期时间 API Optional 类 ...

  8. Java学习:JDK8的新特性

    Java学习:JDK8的新特性 一.十大特性 Lambda表达式 Stream函数式操作流元素集合 接口新增:默认方法与静态方法 方法引用,与Lambda表达式联合使用 引入重复注解 类型注解 最新的 ...

  9. JDK8~13新特性概览

    JDK8 1. 接口default 与 static 关键字 /** * jdk8中接口可以使用声明default和static修饰的方法 * static修饰的方法和普通的方法一样,可以被直接调用 ...

随机推荐

  1. 配置DVWA漏洞环境

    web萌新,因为在别人的环境上练习总有点不舒服,所以在本地搭建了网站:下面记录一下搭建的步骤 DVWA:是一个漏洞环境包,可以用phpstudy或者wamp解析:所以要想配置这个环境,就必须有这两个软 ...

  2. ansible-playbook安装tomcat

    1. ansible-playbook安装tomcat  1) 编写playbook的tomcat安装配置 1 [root@test-1 bin]# vim /ansible/tomcat/bin/t ...

  3. vscode 插件保存记录

  4. K8S节点异常怎么办?TKE"节点健康检查和自愈"来帮忙

    节点健康检测 意义 在K8S集群运行的过程中,节点常常会因为运行时组件的问题.内核死锁.资源不足等各种各样的原因不可用.Kubelet默认对节点的PIDPressure.MemoryPressure. ...

  5. JDBC的学习(一)

    JDBC的学习(一) 概念 所谓英文简写的意思是:Java DataBase Connectivity ,即 Java数据库的连接,用Java语言来操作数据库 本质 简单的来说,就是写这个JDBC的公 ...

  6. 【树形DP】BZOJ 1131 Sta

    题目内容 给出一个\(N\)个点的树,找出一个点来,以这个点为根的树时,所有点的深度之和最大 输入格式 给出一个数字\(N\),代表有\(N\)个点.\(N \le 1000000\).下面\(N-1 ...

  7. Spring源码解析之基础应用(二)

    方法注入 在spring容器中,大部分bean的作用域(scope)是单例(singleton)的,少部分bean的作用域是原型(prototype),如果一个bean的作用域是原型,我们A bean ...

  8. zookeeper-(单机,伪集群)

    安装zookeeper(单机,伪集群):    1.下载 登陆zookeeper官网下载 https://zookeeper.apache.org/  或者  https://mirror.bit.e ...

  9. docker启动服务---------------mysql

    1.查找镜像: docker search mysql 也可以去官网查看镜像tag,选择自己需要的版本,否则会下载最新版本:https://hub.docker.com/_/mysql/ 2.下载镜像 ...

  10. 洛谷 P6419 Kamp 题解

    明天就SX AFO了交篇题解%一下 这题大概是我第一道有独立思考切掉的紫题 之前的都是各种抄借鉴题解 为什么写这题的题解呢?另一个重要的原因是这样的↓ 翻了翻已有题解中的几篇,下面几种情况屡见不鲜 样 ...