阅源-jdk8-FunctionalInterface注解
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注解的更多相关文章
- 关于JAVA中源码级注解的编写及使用
一.注解简介: 1.1.什么是"注解": 在我们编写代码时,一定看到过这样的代码: class Student { private String name; @Override ...
- Java @FunctionalInterface注解-6
在学习 Lambda 表达式时,我们提到如果接口中只有一个抽象方法(可以包含多个默认方法或多个 static 方法),那么该接口就是函数式接口.@FunctionalInterface 就是用来指定某 ...
- Spring源码系列 — 注解原理
前言 前文中主要介绍了Spring中处理BeanDefinition的扩展点,其中着重介绍BeanDefinitionParser方式的扩展.本篇文章承接该内容,详解Spring中如何利用BeanDe ...
- Spring源码之注解扫描Component-scan
本文主要介绍Spring的component-scan标签,了解spring是如何实现扫描注解进行bean的注册,主要实现实在 NamespaceHandler, NamespaceHandlerSu ...
- Spring源码之注解的原理
https://blog.csdn.net/qq_28802119/article/details/83573950 https://www.zhihu.com/question/318439660/ ...
- JDK8新特性:函数式接口@FunctionalInterface的使用说明
我们常用的一些接口Callable.Runnable.Comparator等在JDK8中都添加了@FunctionalInterface注解. 通过JDK8源码javadoc,可以知道这个注解有以下特 ...
- JDK8 新特性
JDK8 新特性目录导航: Lambda 表达式 函数式接口 方法引用.构造器引用和数组引用 接口支持默认方法和静态方法 Stream API 增强类型推断 新的日期时间 API Optional 类 ...
- Java学习:JDK8的新特性
Java学习:JDK8的新特性 一.十大特性 Lambda表达式 Stream函数式操作流元素集合 接口新增:默认方法与静态方法 方法引用,与Lambda表达式联合使用 引入重复注解 类型注解 最新的 ...
- JDK8~13新特性概览
JDK8 1. 接口default 与 static 关键字 /** * jdk8中接口可以使用声明default和static修饰的方法 * static修饰的方法和普通的方法一样,可以被直接调用 ...
随机推荐
- git检出某文件的指定版本
比如当时文件所处的版本id是27e6266d86de3e6da6e1e7a8c43a8b51d6a87032 文件名是system/models/waimai/huodongdiscount.mdl. ...
- 强大的table组件-antd pro table
概述 antd pro table antd pro table 的主要部分 表格显示的配置(绿色框内) 检索的配置(红色框内) 是否显示检索部分 检索的内容是如何生效的 工具栏的配置(黄色框内) 表 ...
- OpenSSL加密系统简介
加密基本原理 OpenSSL移植到arm开发板参考 http://blog.chinaunix.net/uid-27717694-id-3530600.html 1.公钥和私钥: 公钥和私钥就是俗称 ...
- 在Linux命令行内的大小写转换
在编辑文本时大小写常常是需要注意的地方,大小写的转换是很枯燥而繁琐的工作,所幸,Linux 提供了很多能让这份工作变得容易的命令.接下来让我们看看都有哪些完成大小写转换的命令. tr 命令 tr (t ...
- 从面试角度学完 Kafka
Kafka 是一个优秀的分布式消息中间件,许多系统中都会使用到 Kafka 来做消息通信.对分布式消息系统的了解和使用几乎成为一个后台开发人员必备的技能.今天码哥字节就从常见的 Kafka 面试题入手 ...
- 从Linux源码看Socket(TCP)的bind
从Linux源码看Socket(TCP)的bind 前言 笔者一直觉得如果能知道从应用到框架再到操作系统的每一处代码,是一件Exciting的事情. 今天笔者就来从Linux源码的角度看下Server ...
- 【全网免费VIP观看】哔哩哔哩番剧解锁大会员-集合了优酷-爱奇艺-腾讯-芒果-乐视-ab站等全网vip视频免费破解去广告-高清普清电视观看-持续更新
哔哩哔哩番剧解锁大会员-集合了优酷-爱奇艺-腾讯-芒果-乐视-ab站等全网vip视频免费破解去广告-高清普清电视观看-持续更新 前言 突然想看电视,结果 没有VIP 又不想花钱,这免费的不久来啦. 示 ...
- BUUCTF-[极客大挑战 2019]HardSQL 1详解
来到sql注入骚姿势,我们一点一点开始学 我们来到这道题,然后尝试注入,结果发现 拼接'or '1'='1 'or '1'='2如果是字符型注入则会报错,然而并没有而是显示的页面一样, 通过常规注入, ...
- composer 阿里云镜像配置
https://developer.aliyun.com/composer 全局配置(推荐) 所有项目都会使用该镜像地址: composer config -g repo.packagist comp ...
- 运行shell文件时提示/bin/bash^M: bad interpreter: 没有那个文件
查看脚本文件是dos格式还是unix格式的几种办法.(1)cat -A filename 从显示结果可以判断,dos格式的文件行尾为^M$,unix格式的文件行尾为$:(2)od -t x1 file ...