大概是这样,手写没有编译器警告可能会有错误。。但是是能实现的
import java.util.function.*;
class I {
@
Override public String toString() { return "I"; }
public O println() {
System.out.println(this);
return new O();
}
}
class O {
@
Override public String toString() { return "O"; }
public O println() {
System.out.println(this);
return this;
}
}
public class TransformFunction {
static Function<I,O> transform(Function<I,O> in) {
return in.andThen(O::println);
}
public static void main(String[] args) {
Function<I,O> f2 = transform(I::println);
O o = f2.apply(new I());
}
}