含糊不清的groovy:如何解决

时间:2018-01-05 19:35:05

标签: groovy

我试图在groovy

中这样做
def results = new File(someDirectory).listFiles { it.name.startsWith 'foo'}

但是,我得到了可以理解的错误:

groovy.lang.GroovyRuntimeException: Ambiguous method overloading for method java.io.File#listFiles.
Cannot resolve which method to invoke for [class change$_getRollback_closure1] due to overlapping prototypes between:
    [interface java.io.FileFilter]
    [interface java.io.FilenameFilter]

如何告诉它使用FileFilter

我最后写的是普通的java风格:

File[] files = someDir.listFiles( new FileFilter(){
    @Override
    boolean accept(File pathname) {
        return pathname.name.startsWith("foo")
    }
})

1 个答案:

答案 0 :(得分:5)

您可以强制转换或强制关闭,以指定您所指的SAM类型/功能接口。

Float