[英]Java interface &
I can't understand what is it??看不懂是什么??
interface A{
void foo();
}
interface B{
void foo();
}
then what is the result of (A & B)那么 (A & B) 的结果是什么
System.out.println((A & B) () -> {
System.out.println("1");
System.out.println("2");
});
Could someone help me?有人可以帮助我吗?
A & B
is an intersection type. A & B
是交叉类型。 An object of this type can act as both an A
and as a B
.这种类型的对象可以同时充当
A
和B
。
This particular intersection type also happens to be a "functional interface" type, which means that it only has 1 abstract method.这种特殊的交集类型也恰好是“功能接口”类型,这意味着它只有 1 个抽象方法。 This lets you instantiate an object of this type using a lambda-expression.
这使您可以使用 lambda 表达式实例化此类型的对象。
A & B
is a "functional interface" type because both A
and B
have the foo
-method with the exact same type signature. A & B
是“函数式接口”类型,因为A
和B
都具有foo
方法,并且类型签名完全相同。
So that is what this code means.所以这就是这段代码的意思。 You are instantiating an object of type
A & B
, implementing the foo
-method with the lambda-function.您正在实例化
A & B
类型的对象,使用 lambda 函数实现foo
方法。
(A & B) () -> {
System.out.println("1");
System.out.println("2");
}
The System.out.println
will just print out this lambda object using it's toString
-method. System.out.println
将使用它的toString
方法打印出这个 lambda 对象。 Note that it will not call the actual foo
-method.请注意,它不会调用实际的
foo
方法。 Your code will therefore just print out something similar to this: Main$$Lambda$1/1642360923@1376c05c
因此,您的代码将打印出类似于以下内容的内容:
Main$$Lambda$1/1642360923@1376c05c
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.