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类型的接口 ...
随机推荐
- 0012 sublime快捷操作emmet语法
Emmet的前身是Zen coding,它使用缩写,来提高html/css的编写速度. 生成标签 直接输入标签名 按tab键即可 比如 div 然后tab 键, 就可以生成 如果想要生成多个相同标签 ...
- jitamin基于lnmp环境搭建
从github上面下载源代码 cd /Data/apps/nginx/html git clone https://github.com/jitamin/jitamin.git 修改配置文件 cd ...
- (openssh、telnet、vsftpd、nfs、rsync、inotify、samba)
(openssh.telnet.vsftpd.nfs.rsync.inotify.samba) 一:OpenSSH服务与Telnet服务(必须掌握) 前言:OpenSSH是加密传输,Telnet是明文 ...
- docker仓库和dockerfile
通过Dockerfile创建镜像 Dockerfile • Dockerfile语法格式 – FROM:基础镜像 – MAINTAINER:镜像创建者信息 – COPY:复制文件到镜像(所有文 ...
- Shell脚本实现DB2数据库表导出到文件
该Shell脚本用于实现将DB2数据库表导出到文件,将在另一篇博文<Java代码调用Shell脚本并传入参数实现DB2数据库表导出到文件>中通过Java代码实现调用该脚本并传入参数. #! ...
- 原生javascript实现仿QQ延时菜单
一.实现原理 定时器和排他思想 二.代码 <!DOCTYPE html> <html> <head> <title></title> < ...
- TensorFlow——学习率衰减的使用方法
在TensorFlow的优化器中, 都要设置学习率.学习率是在精度和速度之间找到一个平衡: 学习率太大,训练的速度会有提升,但是结果的精度不够,而且还可能导致不能收敛出现震荡的情况. 学习率太小,精度 ...
- Educational Codeforces Round 80 D E
A了三题,rk1000左右应该可以上分啦,开
- APICloud开发者进阶之路 |audioRecorder录音模块Demo
本文出自APICloud官方论坛 audioRecorder 模块通过封装系统的录音接口,能够快速的为开发者提供一个完整的录音功能. 该模块提供Android和iOS版本,录音方式及录制的音频格式 ...
- Netty之缓冲区ByteBuf解读(一)
Netty 在数据传输过程中,会使用缓冲区设计来提高传输效率.虽然,Java 在 NIO 编程中已提供 ByteBuffer 类进行使用,但是在使用过程中,其编码方式相对来说不太友好,也存在一定的不足 ...