为什么在这个 foreach 循环中,elem 不被识别为路径(它确实是路径),而我只能在其上调用对象方法?
public class TypeOption<Path> implements Option<Path> {
@Override
public void apply(String arg, Collection<Path> c) {
for (Path elem : c) {
if (Files.isExecutable(elem)) c.remove(elem);
}
}
}
这一行
if (Files.isExecutable(elem)) c.remove(elem);
它说是造成麻烦的
The method isExecutable(java.nio.file.Path) in the type Files is not applicable for the arguments (Path)
最佳答案
因为Path
此处是一个类型参数 - 您已经声明了一个泛型类型,其中 Path
作为类型参数。我怀疑你*想要:
public class TypeOption implements Option<Path> {
此时,Path
引用名为 Path
的现有类型 ,并用于 Option<T>
的类型参数 (或者 Option
的任何类型参数)。
关于java - 参数化方法内的变量范围 - java 泛型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27647271/