簡體   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