有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

lambda Java 8中使用的功能接口是什么?

我在Java8中遇到了一个新术语:“功能接口”。在处理lambda表达式时,我只能找到它的一种用法

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

例如:

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

除了使用lambda表达式,它在Java8中有多有用

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


共 (6) 个答案

  1. # 1 楼答案

    @FunctionalInterface注释对于代码的编译时检查很有用。除了staticdefault和在@FunctionalInterface或用作函数接口的任何其他接口中重写Object中的方法的抽象方法之外,不能有多个方法

    但是您可以在不使用此注释的情况下使用lambda,也可以在不使用@Override注释的情况下重写方法

    来自医生

    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

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

    public interface Foo {
      public void doSomething();
    }
    

    不能在lambda表达式中使用此

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

    但这将导致编译错误:

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

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

  2. # 2 楼答案

    这个documentation确实对目的产生了影响

    An informative annotation type used to indicate that an interface type declaration is intended to be a functional interface as defined by the Java Language Specification.

    以及用例

    Note that instances of functional interfaces can be created with lambda expressions, method references, or constructor references.

    其措辞一般不排除其他用例。由于主要目的是指示一个函数接口,您的实际问题归结为“除了lambda表达式和方法/构造函数引用之外,函数接口还有其他用例吗?”

    由于功能接口是由Java语言规范定义的Java语言结构,因此只有该规范才能回答这个问题:

    JLS §9.8. Functional Interfaces

    In addition to the usual process of creating an interface instance by declaring and instantiating a class (§15.9), instances of functional interfaces can be created with method reference expressions and lambda expressions (§15.13, §15.27).

    因此,Java语言规范没有给出相反的说明,该部分提到的唯一用例是使用方法引用表达式和lambda表达式创建接口实例。(这包括构造函数引用,因为它们在规范中被称为方法引用表达式的一种形式)

    所以在一句话中,不,在Java8中没有其他的用例

  3. # 3 楼答案

    一点也不。Lambda表达式是该注释的唯一点

  4. # 4 楼答案

    正如其他人所说,功能接口是公开一种方法的接口。它可能有多个方法,但所有其他方法都必须有一个默认实现。它之所以被称为“功能接口”,是因为它实际上是一种功能。由于可以将接口作为参数传递,这意味着函数现在像函数式编程语言一样是“一等公民”。这有很多好处,在使用流API时,您会看到很多好处。当然,lambda表达式是它们的主要明显用途

  5. # 5 楼答案

    lambda表达式可以指定给函数接口类型,但方法引用和匿名类也可以

    关于java.util.function中的特定函数接口的一个优点是,由于它们包含方便的默认方法,它们可以组合起来创建新函数(如Function.andThenFunction.composePredicate.and等)

  6. # 6 楼答案

    只有一个抽象方法的接口称为函数接口。 不强制使用@FunctionInterface,但最好将其与函数接口一起使用,以避免意外添加额外的方法。如果接口用@FunctionInterface注释,并且我们尝试使用多个抽象方法,则会抛出编译器错误

    package com.akhi;
        @FunctionalInterface
        public interface FucnctionalDemo {
    
          void letsDoSomething();
          //void letsGo();      //invalid because another abstract method does not allow
          public String toString();    // valid because toString from Object 
          public boolean equals(Object o); //valid
    
          public static int sum(int a,int b)   // valid because method static
            {   
                return a+b;
            }
            public default int sub(int a,int b)   //valid because method default
            {
                return a-b;
            }
        }