【发布时间】:2012-12-07 10:38:06
【问题描述】:
我正在使用未来的 java 8 版本,即 JDK 1.8。
我发现你可以轻松做到
interface Foo { int method(); }
并像使用它
Foo foo = () -> 3;
System.out.println("foo.method(); = " + foo.method());
只打印 3。
我还发现有一个 java.util.function.Function 接口以更通用的方式执行此操作。但是这段代码不会编译
Function times3 = (Integer triple) -> 3 * triple;
Integer twelve = times3.map(4);
看来我首先必须做一些类似的事情
interface IntIntFunction extends Function<Integer, Integer> {}
IntIntFunction times3 = (Integer triple) -> 3 * triple;
Integer twelve = times3.map(4);
所以我想知道是否有另一种方法可以避免 IntIntFunction 步骤?
【问题讨论】:
-
Mapper<Integer, Integer> times3也许? -
现在我开始了解泛型,它们附带了 THIS :-(...
-
其实在最新的build里,已经没有接口Mapper了。现在叫Function。有一些原始版本称为IntFunction、LongFunction 和DoubleFunction。
-
Function<Integer, Integer> times3使用当前的 JDK8