GroovyShell().parse传递参数 [英] GroovyShell().parse passing parameters
本文介绍了GroovyShell().parse传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Groovy脚本,需要从外部groovy脚本解析一个类.我不确定如何传递参数.这是有效的方法:
I have a groovy script that needs to parse a class from an external groovy script. I am not sure how to pass parameters. Here is what works:
我正在运行的Groovy脚本正在使用此行从external.groovy解析外部类:
Groovy script I am running is using this line to parse the external class from external.groovy:
new GroovyShell().parse(new File('External.groovy'))
这是external.groovy的样子:
Here is what external.groovy looks like:
class External {
public external() {
println "Hello"
}
}
有效.
我遇到的问题,我找不到将参数传递给外部方法的方法.这是external.groovy的外观:
The problem I am having, I cant find a way to pass parameters to the external method. Here is what external.groovy should look like:
class External {
public external(String name) {
println name
}
}
如何向正在运行的脚本中添加参数:
How do I add parameters to the running script:
new GroovyShell().parse(new File('external.groovy')) //need to include the 'Name' parameter to this
推荐答案
-
parse
仅解析您的文件,不执行它 - 您也必须拨打
run
- 您需要实例化文件,并且需要调用您的方法并为其指定参数
- 您需要通过
Binding
对象提供参数
parse
only parses your file and doesn't execute it- you have to call
run
as well - you need to instantiate your file AND you need to call your method and give it the parameter
- you need to give the parameter via an
Binding
object
这是课程和电话
class External {
public external(String name) {
println name
}
}
new External.external(somename)
然后
def bindings = new Binding()
bindings.setVariable("somename", "mrhaki")
def shell = new GroovyShell(bindings)
shell.parse(new File('external.groovy'))
shell.run()
这篇关于GroovyShell().parse传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文