Java8新特性一点通 | 回顾功能接口Functional Interface
Functional Interface
Functional Interface是什么?
功能接口是java 8中的新增功能,它们只允许一个抽象方法。这些接口也称为单抽象方法接口(SAM接口)。这些也可以使用Lambda表达式,方法引用和构造函数引用来表示。Java 8也引入了一个注释,即@FunctionalInterface,当你注释的接口违反了Functional Interface的契约时,它可以用于编译器级错误。
Tips:如果你想强制当前接口只保留一个抽象方法,就可以使用@FunctionalInterface,它可以帮我们通过编译错误提示用户必须不能声明超过一个抽象方法
构建我们第一个默认接口:
@FunctionalInterface
public interface MyFuntinoalInterface<T> {
public void eat();
/**
* 超过一个抽象方法会编译报错,可以把下面注释去掉查看效果
*/
//public void play();
}
尝试将上例的注释去掉,可以发现,超过两个抽象方法会直接报编译错误
@FunctionalInterface
public interface MyFuntinoalInterface<T> {
public void eat();
/**
* 超过一个抽象方法会编译报错,可以把下面注释去掉查看效果
*/
public void play();
}
报如下编译异常:

功能接口的使用原则
- 在任何功能接口中只允许一种抽象方法。功能界面中不允许使用第二种抽象方法。如果我们删除@FunctionInterface注释,那么我们可以添加另一个抽象方法,但它会使接口成为非功能性接口。
- 即使省略
@FunctionalInterface注释,功能接口也是有效的。它仅用于通知编译器在接口内强制执行单个抽象方法。 - 从概念上讲,功能界面只有一种抽象方法。由于默认方法具有实现,因此它们不是抽象的。由于默认方法不是抽象的,因此您可以根据需要随意添加默认方法到功能接口。
你可以添加多个默认方法到功能接口中,但是它不会计入抽象方法的数量:
@FunctionalInterface
public interface MyFuntinoalInterface<T> {
public void eat();
default void run(){
System.out.println("跑步");
}
default void swim(){
System.out.println("游泳");
}
}
- 功能接口里可以有多个覆盖java.lang.Objectd的抽象方法,它不会计入接口的抽象方法数量。
@FunctionalInterface
public interface MyFuntinoalInterface<T> {
public void eat();
/**
* 重写Object方法不会算入抽象方法统计
* @return
*/
@Override
public String toString();
}
JDK 中的功能接口
JDK1.8开始支持功能接口,以下可以举几个例子,功能接口主要是配合lambda表达式使用,具体介绍会在后续的lambda表达式章节也会提到
Comparator
@FunctionalInterface
public interface Comparator<T>
在JDK1.8中,主要对Comparator接口添加了默认方法,对Comparator实现类添加了很多新特性方法
Consumer
@FunctionalInterface
public interface Consumer<T>
相信大家查看JDK1.8源码会发现,很多类都有Consumer这个接口存在,在JDK1.8中更多是提供各个接口支持lambda表达式使用
Iterable
public interface Iterable<T> {
/**
* Returns an iterator over elements of type {@code T}.
*
* @return an Iterator.
*/
Iterator<T> iterator();
/**
* Performs the given action for each element of the {@code Iterable}
* until all elements have been processed or the action throws an
* exception. Unless otherwise specified by the implementing class,
* actions are performed in the order of iteration (if an iteration order
* is specified). Exceptions thrown by the action are relayed to the
* caller.
*
* @implSpec
* <p>The default implementation behaves as if:
* <pre>{@code
* for (T t : this)
* action.accept(t);
* }</pre>
*
* @param action The action to be performed for each element
* @throws NullPointerException if the specified action is null
* @since 1.8
*/
default void forEach(Consumer<? super T> action) {
Objects.requireNonNull(action);
for (T t : this) {
action.accept(t);
}
}
/**
* Creates a {@link Spliterator} over the elements described by this
* {@code Iterable}.
*
* @implSpec
* The default implementation creates an
* <em><a href="Spliterator.html#binding">early-binding</a></em>
* spliterator from the iterable's {@code Iterator}. The spliterator
* inherits the <em>fail-fast</em> properties of the iterable's iterator.
*
* @implNote
* The default implementation should usually be overridden. The
* spliterator returned by the default implementation has poor splitting
* capabilities, is unsized, and does not report any spliterator
* characteristics. Implementing classes can nearly always provide a
* better implementation.
*
* @return a {@code Spliterator} over the elements described by this
* {@code Iterable}.
* @since 1.8
*/
default Spliterator<T> spliterator() {
return Spliterators.spliteratorUnknownSize(iterator(), 0);
}
}
Iterable虽然接口头上没有@FunctionalInterface注解标注,它这里只有一个抽象方法,默认也是一个功能接口,在JDK1.8中新添加了支撑lambda表达式的默认方法
Java8新特性一点通 | 回顾功能接口Functional Interface的更多相关文章
- Java8新特性一点通 | 回顾字符转日期&JoinArray使用
StringToDate日期转换 Join Array使用 StringToDate日期转换 Convert string to date in ISO8601 format 利用LocalDate. ...
- Java8新特性一点通 | 回顾文件操作和watchService文件变更监视操作
文件操作 WatchService 文件操作 今天重温了一些文件操作: - Files.list() 遍历文件和目录 //List all files and sub-directories usin ...
- Java8新特性—四大内置函数式接口
Java8新特性--四大内置函数式接口 预备知识 背景 Lambda 的设计者们为了让现有的功能与 Lambda 表达式良好兼容,考虑了很多方法,于是产生了函数接口这个概念. 什么是函数式接口? 函数 ...
- java8新特性—四大内置核心接口
java8新特性-四大内置核心接口 四大内置核心接口 //消费型接口 Consumer<T>:: vode accept(T t); //供给型接口 Supplier<T>:: ...
- Java8新特性_lambda表达式和函数式接口最详细的介绍
Lambda表达式 在说Lambda表达式之前我们了解一下函数式编程思想,在数学中,函数就是有输入量.输出量的一套计算方案,也就是“拿什么东西做什么事情”. 相对而言,面向对象过分强调“必须通过对象的 ...
- Java8新特性时间日期库DateTime API及示例
Java8新特性的功能已经更新了不少篇幅了,今天重点讲解时间日期库中DateTime相关处理.同样的,如果你现在依旧在项目中使用传统Date.Calendar和SimpleDateFormat等API ...
- Java8新特性(一)——Lambda表达式与函数式接口
一.Java8新特性概述 1.Lambda 表达式 2. 函数式接口 3. 方法引用与构造器引用 4. Stream API 5. 接口中的默认方法与静态方法 6. 新时间日期 API 7. 其他新特 ...
- JAVA8新特性——接口定义增强
JAVA9都要出来了,JAVA8新特性都没搞清楚,是不是有点掉队哦~ 接口定义增强 在JDK1.8以前,接口是定义的: 接口(英文:Interface),在JAVA编程语言中是一个抽象类型,是抽象方法 ...
- java8新特性学习:函数式接口
本文概要 什么是函数式接口? 如何定义函数式接口? 常用的函数式接口 函数式接口语法注意事项 总结 1. 什么是函数式接口? 函数式接口其实本质上还是一个接口,但是它是一种特殊的接口:SAM类型的接口 ...
随机推荐
- 2019 ICPC南昌网络赛 B题
英雄灭火问题忽略了一点丫 一个超级源点的事情,需要考虑周全丫 2 #include<cstdio> #include<cstring> #include<queue> ...
- node多进程的创建与守护
node是单线程运行,我们的node项目如何利用多核CPU的资源,同时提高node服务的稳定性呢? 1. node的单线程 进程是一个具有一定独立功能的程序在一个数据集上的一次动态执行的过程,是操作系 ...
- Spark学习笔记(二)—— Local模式
Spark 的运行模式有 Local(也称单节点模式),Standalone(集群模式),Spark on Yarn(运行在Yarn上),Mesos以及K8s等常用模式,本文介绍第一种模式. 1.Lo ...
- PHP计算每月几周,每周的开始结束日期
PHP计算每月几周,每周的开始结束日期 因为项目中需要一个每周工作计算的功能,具体日期的算法是,把每月拆分成几个周,最后一个星期这个月份的天数不够就补上下个月的. 列如今天8月27星期一,这个月有31 ...
- Linux中的零拷贝
零拷贝 本文图片和一些内容均来自后面的参考,非原创只是把文章中的一些关键内容整理一下,算作是一个学习笔记. 传统的I/O操作 传统的IO操作是用户应用程序只是需要调用两个系统调用 read() 和 w ...
- PS/2的相关知识
PS/2接口 很多微机上采用PS/2口来连接鼠标和键盘.PS/2接口与传统的键盘接口除了在接口外型.引脚有不同外,在数据传送格式上是相同的.现在很多主板用PS/2接口插座连接键盘,传统接口的键盘可以通 ...
- DZNEmptyDataSet框架阅读
前段时间使用公司封装的空白页占位视图工具,工具是对DZNEmptyDataSet框架的封装.这个框架以前在许多项目也都用过,却没有认真阅读过源码,真的很遗憾.这两天趁五一放假有空,将DZNEmpt ...
- Codeforces - A. Watermelon
A. Watermelon time limit per test 1 second memory limit per test 64 megabytes input standard input o ...
- python文档字符串(函数使用说明)
关键字: 函数说明.help()函数 1.效果图: 2.代码: # 文档字符串( doc str) 是 函数使用说明 # 用法: 在函数第一行写一个字符串 def fn(*nums): ''' 函数的 ...
- 【DPDK】【ring】从DPDK的ring来看无锁队列的实现
[前言] 队列是众多数据结构中最常见的一种之一.曾经有人和我说过这么一句话,叫做“程序等于数据结构+算法”.因此在设计模块.写代码时,队列常常作为一个很常见的结构出现在模块设计中.DPDK不仅是一个加 ...