为了测试功能接口 Function 的 compose() 和 andThen() 方法,我做了一个简单的代码:
public class Test {
public Trans testCompose() {
return new T1().compose(new T2());
}
}
interface Trans extends Function<File, File> {
}
class T1 implements Trans {
@Override
public File apply(File file) {
return null;
}
}
class T2 implements Trans {
@Override
public File apply(File file) {
return null;
}
}
但是正如你所看到的,这段代码不会编译。给定原因:不存在类型 variable(v) V 的实例,因此 Function 符合 Trans。
关注多个关于通配符中扩展和超级差异的主题,我了解它们在列表中的使用,但不确定为什么它不能在这里编译。
有什么方法可以在 testCompose() 方法中继续返回对象 Trans 还是必须返回一个 Function 来让代码编译?