Lambda 练习
Lambda exercise
我正在做一些 Java OCA 测试模拟。我不明白这个练习的答案:
interface Climb {
boolean isTooHigh(int height, int limit);
}
public class Lambdas {
public static void main(String[] args) {
check((h,l) -> l, 5);
}
private static void check(Climb climb, int height) {
if (climb.isTooHigh(height, 10))
System.out.println("Too");
else
System.out.println("ok");
}
}
我不明白 l
是 "l" 还是“1”,因为拼写有点难以理解。我也不明白这个练习的逻辑。
你能告诉我需要什么来更正形式:l 或 1 吗?
你能给我解释一下这个练习吗?
非常感谢!
这是正在发生的事情:
(h,l) -> l
是一个采用 h
和 l
以及 returns l
的 lambda。根据interface Climb
,h
和l
作为参数是int
,返回的l
应该是bool
;似乎有一个隐式类型转换。
因此行 check((h,l) -> l, 5);
没问题,并采用正确类型的参数 (Climb , int)
。
(h,l) -> l
正在做:"let's forget about h, and if l != 0, return true else if l == 0 return false
".
PS:如果是1
,答案会一直是true
更正后的版本
interface Climb {
boolean isTooHigh(int height, int limit);
}
public class Main {
public static void main(String[] args) {
check((h,l) -> h>l, 5);
}
private static void check(Climb climb, int height) {
System.out.println(climb.isTooHigh(height, 10) ? "Too":"ok" );
}
}
我正在做一些 Java OCA 测试模拟。我不明白这个练习的答案:
interface Climb {
boolean isTooHigh(int height, int limit);
}
public class Lambdas {
public static void main(String[] args) {
check((h,l) -> l, 5);
}
private static void check(Climb climb, int height) {
if (climb.isTooHigh(height, 10))
System.out.println("Too");
else
System.out.println("ok");
}
}
我不明白 l
是 "l" 还是“1”,因为拼写有点难以理解。我也不明白这个练习的逻辑。
你能告诉我需要什么来更正形式:l 或 1 吗? 你能给我解释一下这个练习吗?
非常感谢!
这是正在发生的事情:
(h,l) -> l
是一个采用 h
和 l
以及 returns l
的 lambda。根据interface Climb
,h
和l
作为参数是int
,返回的l
应该是bool
;似乎有一个隐式类型转换。
因此行 check((h,l) -> l, 5);
没问题,并采用正确类型的参数 (Climb , int)
。
(h,l) -> l
正在做:"let's forget about h, and if l != 0, return true else if l == 0 return false
".
PS:如果是1
,答案会一直是true
更正后的版本
interface Climb {
boolean isTooHigh(int height, int limit);
}
public class Main {
public static void main(String[] args) {
check((h,l) -> h>l, 5);
}
private static void check(Climb climb, int height) {
System.out.println(climb.isTooHigh(height, 10) ? "Too":"ok" );
}
}