在Java 8中,看起来类的lambda保存在数组中.例如,假设我们有这个类:
public class LambdaFactory {
public Supplier<Integer> getOne(){
return () -> 42;
}
public Supplier<Integer> getTwo(){
return () -> 128;
}
public Supplier<Integer> getThree(){
return () -> 3;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我这样打印出来:
System.out.println(factory.getOne());
System.out.println(factory.getOne());
System.out.println(factory.getTwo());
System.out.println(factory.getThree());
Run Code Online (Sandbox Code Playgroud)
输出将是这样的
examples.LambdaFactory$$Lambda$1@4e515669
examples.LambdaFactory$$Lambda$1@4e515669
examples.LambdaFactory$$Lambda$2@1b9e1916
examples.LambdaFactory$$Lambda$3@ba8a1dc
Run Code Online (Sandbox Code Playgroud)
所以我们在这里可以看到两件事.两次调用的lambda给了我们相同的lambda对象(这与我们每次都可以得到一个新的内部类不同).我们也看到他们看起来像是被保留在某种类型的"Lambda"结构中
我的问题是,我可以在课堂上看到lambdas吗?我没有任何理由这样做,我只是喜欢解剖一些东西