函数接口,是指内部只有一个抽象方法的接口。
注意关键词:只有一个,抽象方法,接口。
我们声明一个接口,如果满足这个条件,就是函数式接口;编译器会自行检测这个接口是否是一个函数式接口(并不是简单的统计方法数量,是看上面的三个条件),我们也可以显示的使用@FunctionalInterface指定这个接口;但是,如果接口不满足条件,添加@FunctionalInterface注解时,会报错。
常见的Runnable接口,就是一个标准的函数式接口。
@FunctionalInterface
public interface Runnable {
/**
* When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object's
* <code>run</code> method to be called in that separately executing
* thread.
* <p>
* The general contract of the method <code>run</code> is that it may
* take any action whatsoever.
*
* @see java.lang.Thread#run()
*/
public abstract void run();
}
我们也可以自定义一个函数式接口:
package com.java4all.mypoint;
/**
* Author: yunqing
* Date: 2018/7/18
* Description:自定义函数式接口
*/
@FunctionalInterface
public interface MyFuncInter {
abstract Integer trans();
public static void get(){
System.out.println("测试啊");
}
}
但是,看了几十篇博客,还是不了解,对于日常开发而言,我们自定义函数式接口有什么用?结合lambda使用?
如果有比较了解:《自定义函数式接口如何优化了或者改进了日常开发?》这个问题的,还请赐教。