我是Java的新手,现在正在学习The Java Tutorials。当我在LabmdaScopeTest.java
中输入确切的代码时,我遇到了错误package java.util.funciton does not exist
。代码如下。
import java.util.funciton.Consumer;
public class LambdaScopeTest {
public int x = 0;
class FirstLevel {
public int x = 1;
void methodInFirstLevel(int x) {
// The following statement causes the complier to generate
// the error "local variables referenced from a lambda expression
// must be final or effectively final" in statemen A:
//
// x = 99;
Consumer<Integer> myConsumer = (y) ->
{
System.out.println("x = " + x); // statement A
System.out.println("y = " + y);
System.out.println("this.x = " + this.x);
System.out.println("LambdaScopeTest.this.x = " +
LambdaScopeTest.this.x);
};
myConsumer.accept(x);
}
}
public static void main(String[] args) {
LambdaScopeTest st = new LambdaScopeTest();
LambdaScopeTest.FirstLevel fl = st.new FirstLevel();
fl.methodInFirstLevel(23);
}
}
我对import
语句进行了评论,并将代码替换为java.util.function.Consumer<Integer> myConsumer
,一切正常。
答案 0 :(得分:8)
你有一个错字:
import java.util.funciton.Consumer;
↑↑
应该是:
import java.util.function.Consumer;