繁体   English   中英

参数化方法内部的变量范围-Java泛型

[英]variable scope inside of parametrized method - java generics

为什么在此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);
        }
    }
}

这条线

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 )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM