Java 8 中使用的函数式接口是什么? [英] What are functional interfaces used for in Java 8?

查看:29
本文介绍了Java 8 中使用的函数式接口是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Java 8 中遇到了一个新术语:函数式接口".在使用 lambda 表达式 时,我只能找到它的一种用法.

Java 8 提供了一些内置的函数式接口,如果我们想定义任何函数式接口,那么我们可以使用 @FunctionalInterface 注释.它将允许我们在接口中只声明一个方法.

例如:

@FunctionalInterface接口 MathOperation {int 操作(int a, int b);}

它在 Java 8 中除了使用 lambda 表达式之外还有什么用处?

(问题这里 与我问的不同.它是在问为什么我们在使用 lambda 表达式时需要函数式接口.我的问题是:除了用于 lambda 表达式之外,函数式接口还有哪些其他用途?)

解决方案

@FunctionalInterface 注释对于代码的编译时检查很有用.除了 staticdefault 和覆盖 @FunctionalInterfaceObject 方法的抽象方法之外,您不能拥有多个方法> 或用作功能接口的任何其他接口.

但是您可以在没有此注释的情况下使用 lambda,也可以在没有 @Override 注释的情况下覆盖方法.

来自文档

<块引用>

一个函数式接口只有一个抽象方法.由于默认方法有一个实现,它们不是抽象的.如果一个接口声明一个抽象方法,覆盖了以下公共方法之一java.lang.Object,这也不计入接口的抽象方法计数,因为接口的任何实现都会有来自 java.lang.Object 或其他地方的实现

这个可以在 lambda 表达式中使用:

公共接口 Foo {public void doSomething();}

不能在 lambda 表达式中使用:

公共接口 Foo {public void doSomething();公共无效 doSomethingElse();}

但这会导致编译错误:

@FunctionalInterface公共接口 Foo {public void doSomething();公共无效 doSomethingElse();}

<块引用>

'@FunctionalInterface' 注释无效;Foo 不是函数式界面

I came across a new term in Java 8: "functional interface". I could only find one use of it while working with lambda expressions.

Java 8 provides some built-in functional interfaces and if we want to define any functional interface then we can make use of the @FunctionalInterface annotation. It will allow us to declare only a single method in the interface.

For example:

@FunctionalInterface
interface MathOperation {
    int operation(int a, int b);
}

How useful it is in Java 8 other than just working with lambda expressions?

(The question here is different from the one I asked. It is asking why we need functional interfaces while working with lambda expressions. My question is: What are the other uses of functional interfaces besides use with lambda expressions?)

解决方案

@FunctionalInterface annotation is useful for compilation time checking of your code. You cannot have more than one method besides static, default and abstract methods that override methods in Object in your @FunctionalInterface or any other interface used as a functional interface.

But you can use lambdas without this annotation as well as you can override methods without @Override annotation.

From docs

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

This can be used in lambda expression:

public interface Foo {
  public void doSomething();
}

This cannot be used in lambda expression:

public interface Foo {
  public void doSomething();
  public void doSomethingElse();
}

But this will give compilation error:

@FunctionalInterface
public interface Foo {
  public void doSomething();
  public void doSomethingElse();
}

Invalid '@FunctionalInterface' annotation; Foo is not a functional interface

这篇关于Java 8 中使用的函数式接口是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆