问题描述
在阅读文章 状态Lambda 我来到了目标输入主题,我对以下段落有些困惑:
While reading the article State of the Lambda I came to the topic Target Typing and I'm getting a bit confused with the following paragraph:
此方法的含义是相同的lambda表达式可以 在不同的上下文中具有不同的类型:
An implication of this approach is that the same lambda expression can have different types in different contexts:
Callable<String> c = () -> "done";
PrivilegedAction<String> a = () -> "done";
在第一种情况下,lambda表达式() -> "done"
表示一个 Callable
的实例.在第二种情况下,相同的表达式 表示PrivilegedAction的实例.
In the first case, the lambda expression () -> "done"
represents an instance of Callable
. In the second case, the same expression represents an instance of PrivilegedAction.
编译器负责推断每个lambda的类型 表达.它使用在以下情况下期望的类型: 表达出现;这种类型称为目标类型. λ 表达式只能出现在目标类型为 功能界面.
The compiler is responsible for inferring the type of each lambda expression. It uses the type expected in the context in which the expression appears; this type is called the target type. A lambda expression can only appear in a context whose target type is a functional interface.
您能否以简单的方式向我解释与引用的段落有关的这些要点:
Can you explain me these points in relation with the quoted paragraph in a simple way:
- 目标类型
- 上下文
如果您还提供代码段,我将不胜感激.
I will really appreciate it if you also provide code snippets.
推荐答案
上下文
上下文是在代码中使用表达式的方式.这不仅仅是lambda表达式-它是任何表达式,例如a+b
,a++
或Math.random()
.
Context
Context is the way an expression is used within the code. It's not just lambda expressions - it's any expression, like a+b
, a++
or Math.random()
.
可能的上下文示例:
-
分配:采用表达式
a+b
.如果将其分配给变量,则会在分配上下文中使用它:
Assignment: take the expression
a+b
. If you assign it to a variable, it is used in an assignment context:
c = a+b;
方法或构造函数的参数:.这是当您将其传递给某个方法调用时:
Argument to a method or constructor:. This is when you pass it to some method call:
System.out.println(a+b);
返回值:在return
语句中使用表达式时:
Return value: When you are using the expression in a return
statement:
return a+b;
数组索引:当您的表达式是数组索引时:
Index to an array: When your expression is the index of an array:
x[a+b] = 3;
目标类型是在给定上下文中预期的类型.例如,如果您将方法定义为:
The target type is the type expected in the given context. For example, if you have a method defined as:
public int myMethod() { ... }
,则其主体中return
语句中的任何表达式都应具有类型int
.所以,如果你有这个:
then any expression in a return
statement in its body is expected to have the type int
. So if you have this:
return a+b;
在myMethod
内部,预计a+b
将解析为int
或可分配给int的内容.
inside myMethod
, it's expected that a+b
will resolve to an int
or something that's assignable to an int.
现在,假设您具有此方法:
Now, suppose you have this method:
public void anotherMethod( double d );
然后,当您调用它并传递一个表达式作为参数时,该表达式的类型应为double
.像这样的呼叫:
Then when you call it, and pass an expression as an argument, that expression is expected to be of type double
. So a call like:
anotherMethod(a+b);
期望a+b
解析为double
.这就是它的目标类型.
expects a+b
to resolve to a double
. That's its target type.
在声明中:
Callable<String> c = () -> "done";
表达式是lambda表达式() -> "done"
.它在赋值上下文中使用(已分配给c
).而目标类型是Callable<String>
,因为这是将任何内容分配给c
时所期望的.
the expression is the lambda expression () -> "done"
. It is used in an assignment context (it is assigned to c
). And the target type is Callable<String>
because that's what is expected when you assign anything to c
.
有关更正式的讨论,请参考 Java语言规范,第5章.
For a more formal discussion, refer to the Java Language Specification, Chapter 5.
这篇关于Java 8:目标输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!