用于多种方法的Lambda表达式 [英] Lambda expressions for interface with multiple methods

查看:91
本文介绍了用于多种方法的Lambda表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Java 8 lambda游玩. 当我在界面中添加其他方法时,为什么这会给我一个错误:

Monkeying around with Java 8 lambdas. Why does this give me an error when I add another method to my interface:

      interface Something {
  public String doit(Integer i);
  public int getID(String name);.....

        Something s = (Integer i) -> {
        return i.toString();
    };
    System.out.println(s.doit(4));

    Something y = (Integer i) -> {
        return "do nothing";
    };
    System.out.println(y.doit(4));

无需第二种方法即可正常工作:"public int getID(String name)

Works fine without the second method: "public int getID(String name)

推荐答案

Java lambda和方法引用只能分配给功能接口.从Java SE 8 API中,对

Java lambdas and method references may only be assigned to a functional interface. From Java SE 8 API, description of java.util.function package:

每个功能接口都有一个单一的抽象方法,称为该功能接口的功能方法,lambda表达式的参数和返回类型与之匹配或适配.功能接口可以在多个上下文中提供目标类型,例如分配上下文,方法调用或强制转换上下文:

Each functional interface has a single abstract method, called the functional method for that functional interface, to which the lambda expression's parameter and return types are matched or adapted. Functional interfaces can provide a target type in multiple contexts, such as assignment context, method invocation, or cast context:

JLS 9.8 还讨论了这一点:

功能接口是仅具有一个抽象方法(除Object的方法之外)的接口,因此表示单个功能协定.这个单"表示方法可以采用具有从超级接口继承的等效替代签名的多个抽象方法的形式;在这种情况下,继承的方法在逻辑上表示单个方法.

A functional interface is an interface that has just one abstract method (aside from the methods of Object), and thus represents a single function contract. This "single" method may take the form of multiple abstract methods with override-equivalent signatures inherited from superinterfaces; in this case, the inherited methods logically represent a single method.

对于接口I,让M为抽象方法集,这些抽象方法是I的成员,这些签名与类Object的任何公共实例方法没有相同的签名.然后,如果M中存在方法m,并且满足以下两个条件,则我是一个函数接口:

For an interface I, let M be the set of abstract methods that are members of I that do not have the same signature as any public instance method of the class Object. Then, I is a functional interface if there exists a method m in M for which both of the following are true:

  • m的签名是M中每个方法签名的子签名(第8.4.2节).

  • The signature of m is a subsignature (§8.4.2) of every method's signature in M.

m都是返回类型可替换的(第8.4.5节).

m is return-type-substitutable (§8.4.5) for every method in M.

除了通过声明和实例化类来创建接口实例的常规过程(第15.9节)之外,还可以使用方法引用表达式和lambda表达式(第15.13节,第15.27节)创建功能接口的实例.

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).

泛型会产生有趣的效果:

An interesting effect occurs with generics:

在下面的接口层次结构中,Z 是一个功能性接口 [强调我的],因为Z继承了两个不是Object成员的抽象方法,但它们具有相同的签名,因此在逻辑上继承的方法代表一种方法:

In the following interface hierarchy, Z is a functional interface [emphasis mine] because while it inherits two abstract methods which are not members of Object, they have the same signature, so the inherited methods logically represent a single method:

interface X { int m(Iterable<String> arg); }
interface Y { int m(Iterable<String> arg); }
interface Z extends X, Y {}

这篇关于用于多种方法的Lambda表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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