Groovy 如何在不首先指定变量的情况下调用shell.parse()上的函数?例如shell.parse().someFunction()

Groovy 如何在不首先指定变量的情况下调用shell.parse()上的函数?例如shell.parse().someFunction(),groovy,Groovy,现在我正在做这个: //main.groovy def func = shell.parse( new File('func.groovy') ) func.someMethod('sdfsdfsdfsdf') //func.groovy def someMethod(deploymentFolder) { return deploymentFolder } 我想将main.groovy中的代码段设置为一行程序,但这不起作用: def func = shell.parse( new

现在我正在做这个:

//main.groovy
def func = shell.parse( new File('func.groovy') )
func.someMethod('sdfsdfsdfsdf')

//func.groovy
def someMethod(deploymentFolder) {
    return deploymentFolder
}
我想将main.groovy中的代码段设置为一行程序,但这不起作用:

def func = shell.parse( new File('func.groovy') ).someMethod('sdfsdfsdf')
这也不管用

def func = shell.parse( new File('func.groovy') ) someMethod('sdfsdfsdf')
有没有办法直接调用shell.parse返回的函数

编辑:我正在打对方付费电话,这似乎改变了一切

所以这是行不通的:

list = arrList.collect { file ->
    return shell.parse( new File(file) ).someMethod('sdfsdfsdf')
}
someMethod()返回一个arrayList。在collect之后,虽然列表似乎包含了正确数量的嵌套列表,但它们都是空的

但这样做实际上是可行的:

myarr = []
list = arrList.collect { file ->
    tempVar = shell.parse( new File(file) )
    myarr += tempVar.someMethod('sdfsdfsdf')
}

我不知道有什么区别。我以为collect也会这么做。它似乎做了几乎相同的事情,但它连接的列表都是空的

您在那里的第一次尝试是正确的,并且工作正常:

def shell = new GroovyShell()
println(["func.groovy"].collect{ it as File }.collect{ shell.parse(it).someMethod('sdfsdfsdfsdf') })
// ⇒ [sdfsdfsdfsdf]

def func
将是某种方法的结果-您的第一个版本似乎没有问题。请在你的问题中加上“不起作用”的意思。这仍然很有效。你能分享你的东西吗,这不是预期的工作?例如,您使用的groovy版本等。