我认为这个问题已经存在,但我无法找到它。
我不明白,为什么有必要使用功能界面来处理lambdas。请考虑以下示例:
public class Test {
public static void main(String...args) {
TestInterface i = () -> System.out.println("Hans");
// i = (String a) -> System.out.println(a);
i.hans();
// i.hans("Hello");
}
}
public interface TestInterface {
public void hans();
// public void hans(String a);
}
这没有问题,但如果您取消注释注释行,它就不会。为什么?在我的理解中,编译器应该能够区分这两种方法,因为它们具有不同的输入参数。为什么我需要一个功能界面并炸掉我的代码?
编辑:链接的副本没有回答我的问题,因为我询问不同的方法参数。但是我在这里得到了一些非常有用的答案,感谢所有帮助过的人! :)
EDIT2:对不起,我显然不是以母语为母语的人,而是为了准确自己:
public interface TestInterface {
public void hans(); //has no input parameters</br>
public void hans(String a); //has 1 input parameter, type String</br>
public void hans(String a, int b); //has 2 input parameters, 1. type = String, 2. type = int</br>
public void hans(int a, int b); //has also 2 input parameters, but not the same and a different order than `hans(String a, int a);`, so you could distinguish both
}
public class Test {
public static void main(String...args) {
TestInterface i = () -> System.out.println("Hans");
i = (String a) -> System.out.println(a);
i = (String a, int b) -> System.out.println(a + b);
i = (int a, int b) -> System.out.println(a);
i.hans(2, 3); //Which method would be called? Of course the one that would take 2 integer arguments. :)
}
}
所有我要问的是关于这些论点。方法名称并不重要,但每种方法都采用不同参数的唯一顺序,因此,Oracle可以实现此功能,而不是仅按照&#34; Lambda-Interface&#34;进行单一方法。
答案 0 :(得分:31)
当你写:
TestInterface i = () -> System.out.println("Hans");
您为void hans()
的{{1}}方法提供了实现。
如果可以将lambda表达式分配给具有多个抽象方法的接口(即非功能接口),则lambda表达式只能实现其中一个方法,而其他方法则不实现。
您无法通过将两个具有不同签名的lambda表达式分配给同一个变量来解决它(就像您可以将两个对象的引用分配给单个变量并期望该变量引用这两个对象一样立刻)。
答案 1 :(得分:19)
他们必须只包含一种方法的最重要原因是,否则很容易产生混淆。如果接口中允许多个方法,如果参数列表相同,那么lambda选择哪个方法?
interface TestInterface {
void first();
void second(); // this is only distinguished from first() by method name
String third(); // maybe you could say in this instance "well the return type is different"
Object fourth(); // but a String is an Object, too !
}
void test() {
// which method are you implementing, first or second ?
TestInterface a = () -> System.out.println("Ido mein ado mein");
// which method are you implementing, third or fourth ?
TestInterface b = () -> "Ido mein ado mein";
}
答案 2 :(得分:5)
您似乎在寻找匿名课程。以下代码有效:
public class Test {
public static void main(String...args) {
TestInterface i = new TestInterface() {
public void hans() {
System.out.println("Hans");
}
public void hans(String a) {
System.out.println(a);
}
};
i.hans();
i.hans("Hello");
}
}
public interface TestInterface {
public void hans();
public void hans(String a);
}
Lambda表达式(大多数)是一种只用一种方法编写匿名类的简短方法。 (同样,匿名类是您只在一个地方使用的内部类的简写)
答案 3 :(得分:3)
您无需创建功能界面即可创建lambda函数。该接口允许您为将来的函数调用创建实例。
在您的情况下,您可以使用现有的界面Runable
Runnable r = () -> System.out.println("Hans");
然后致电
r.run();
你可以认为lambda ->
只是简写:
Runnable r = new Runnable() {
void run() {
System.out.println("Hans");`
}
}
使用lambda,您不需要匿名类,这是在上面的示例中创建的。
但是这有一些限制,为了弄清楚应该用什么方法调用lambdas使用的接口必须是SAM(单一抽象方法)。那我们只有一种方法。
有关更详细的说明,请阅读:
Introduction to Functional Interfaces – A Concept Recreated in Java 8
答案 4 :(得分:1)
在java中实现接口时,需要实现它的所有抽象方法(否则实现类就必须是接口)。
Java 编译器使用包含方法的类定义和用于实例化此类的语句在内部扩展 lambda 表达式。目前,java 不支持/提供将 1 个以上的 lambda 与 1 个接口关联的方法。
public class Test {
public static void main(String...args) {
TestInterface i = () -> System.out.println("Hans"); // this will not compile as the implementation for public void hans(String a); can not be provided/is not found
//i = (String a) -> System.out.println(a); //this can not add an implementation for 2nd method to i after compilation of 1st lambda
}
}
public interface TestInterface {
public void hans();
public void hans(String a);
}
这是 java 中的 lambda 只适用于具有一种方法或功能接口的接口的原因。
答案 5 :(得分:0)
根据java specs,功能接口只能只包含一种抽象方法。
当然可以像注释的代码一样一次性使用lambda表达式,但是在将lambda表达式作为参数传递给模拟函数回调时,必须使用函数接口,因为在这种情况下,变量数据类型是函数接口。
例如,email.to
是内置的功能接口:
Runnable
用法如下所示:
public interface Runnable() {
public void run();
}
结果是:
public class LambdaTest {
// data type of parameter 'task' is functional interface 'Runnable'
static void doSeveralTimes(Runnable task, int repeat) {
for (int i = 0; i < repeat; i++) {
task.run();
}
}
public static void main(String[] args) {
// one-time lambda
doSeveralTimes(() -> {
System.out.println("one-time lambda");
}, 3);
// lambda as variable
Runnable test;
test = () -> {
System.out.println("lambda as variable");
};
doSeveralTimes(test, 3);
}
}
答案 6 :(得分:0)
lambda表达式不过是定义功能接口实现的捷径。 它等效于功能接口实现的实例。