蔡香满屋
站在牛顿头上吃苹果

1.什么是函数式接口:

函数式接口(Functional Interface)就是一个有且仅有一个抽象方法,但是可以有多个非抽象方法的接口。函数式接口可以被隐式转换为lambda表达式

函数式接口API。

标准函数式代码有:

package com.test.cgb;
@FunctionalInterface
public interface StandarFunctionInterface {
	void test(); // 默认公共抽象方法
	static void test1() {System.out.println("这是静态方法");};
	default void test2() {System.out.println("这是default方法");};
}

JDK1.8之前已有的函数式接口有:

java.lang.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();
}

java.util.Comparator如:

@FunctionalInterface
public interface Comparator<T> {}

java.util.cocurrent.Callable如:

@FunctionalInterface
public interface Callable<V> {
    /**
     * Computes a result, or throws an exception if unable to do so.
     *
     * @return computed result
     * @throws Exception if unable to compute a result
     */
    V call() throws Exception;
}

  java.io.FileFilter如:

@FunctionalInterface
public interface FileFilter {

    /**
     * Tests whether or not the specified abstract pathname should be
     * included in a pathname list.
     *
     * @param  pathname  The abstract pathname to be tested
     * @return  <code>true</code> if and only if <code>pathname</code>
     *          should be included
     */
    boolean accept(File pathname);
}

JDK1.8新增加的函数接口:

java.util.function此包中包含了很多类,用来支持Java的函数式编程。

2.函数式接口注解:

@FunctionInterface:我们在函数式接口上加上此注解后,里面就只能够有一个抽象方法了。如果不满足条件,则会报错。

posted on 2020-02-27 00:30  蔡香满屋  阅读(145)  评论(0)    收藏  举报