[英]variable scope inside of parametrized method - java generics
why is it that inside this foreach loop, elem is not recognized as a Path, which it is, and I can only call Object methods on it? 为什么在此foreach循环中,elem不能被识别为Path,而我只能在其上调用Object方法?
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);
}
}
}
this line 这条线
if (Files.isExecutable(elem)) c.remove(elem);
is causing the trouble, it says 造成麻烦,它说
The method isExecutable(java.nio.file.Path) in the type Files is not applicable for the arguments (Path)
It's because Path
is a type parameter here - you've declared a generic type, with Path
as the type parameter. 这是因为
Path
是这里的类型参数-您已经声明了泛型,而Path
是类型参数。 I suspect you *wanted: 我怀疑你*想要:
public class TypeOption implements Option<Path> {
At that point, Path
refers to the existing type called Path
, and is used for the type argument to Option<T>
(or whatever the type parameter is for Option
). 在这一点上,
Path
是指称为现有类型 Path
,以及用于类型参数 Option<T>
或任何类型参数为Option
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.