包 java.util.function 不存在?
package java.util.function does not exist?
我是 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
,一切都变得正常了。
你打错了:
import java.util.funciton.Consumer;
↑↑
应该是:
import java.util.function.Consumer;
我是 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
,一切都变得正常了。
你打错了:
import java.util.funciton.Consumer;
↑↑
应该是:
import java.util.function.Consumer;