Variables 在其他脚本中定义的调用闭包
访问Groovy中另一个脚本中定义的闭包的正确和最简单的方法是什么。这可能不是最好的设计,但我在中定义了一个闭包 SomeScript.groovyVariables 在其他脚本中定义的调用闭包,variables,binding,groovy,Variables,Binding,Groovy,访问Groovy中另一个脚本中定义的闭包的正确和最简单的方法是什么。这可能不是最好的设计,但我在中定义了一个闭包 SomeScript.groovy bindingC = { ... } def localC = { ... } SomeScript s = new SomeScript() s.bindingC(...); s.localC(...); OtherScript.groovy bindingC = { ... } def localC = {
bindingC = {
...
}
def localC = {
...
}
SomeScript s = new SomeScript()
s.bindingC(...);
s.localC(...);
OtherScript.groovy
bindingC = {
...
}
def localC = {
...
}
SomeScript s = new SomeScript()
s.bindingC(...);
s.localC(...);
注意:SomeScript.groovy是程序逻辑,而OtherScript.groovy是单元测试逻辑。它们都在同一个包中,我已经能够访问SomeScript中的方法了。有两种方法(我知道)可以用来实现您想要的;您可以实例化SomeScript
并运行其内容,也可以使用groovyshell计算SomeScript
。需要执行脚本,以便在绑定中创建变量:
SomeScript.groovy:
bindingC = {
110.0
}
OtherScript.groovy解决方案1:
s = new GroovyShell().evaluate new File("SomeScript.groovy")
assert s.bindingC() == 110.0
OtherScript.groovy解决方案2:
s2 = new SomeScript() // SomeScript is also an instance of groovy.lang.Script
s2.run()
assert s2.bindingC() == 110.0
有两种方法(我知道)你可以用来实现你想要的;您可以实例化SomeScript
并运行其内容,也可以使用groovyshell计算SomeScript
。需要执行脚本,以便在绑定中创建变量:
SomeScript.groovy:
bindingC = {
110.0
}
OtherScript.groovy解决方案1:
s = new GroovyShell().evaluate new File("SomeScript.groovy")
assert s.bindingC() == 110.0
OtherScript.groovy解决方案2:
s2 = new SomeScript() // SomeScript is also an instance of groovy.lang.Script
s2.run()
assert s2.bindingC() == 110.0
本地关闭是不可访问的,对吗?因为它类似于方法的局部变量。@xst,还有groovy转换
@字段
,这可以更改脚本变量的范围:因此@Field
是ScriptXXX类上的私有def var
,除了私有def var
在脚本中不允许,而@Field
是。本地闭包不可访问,对吗?因为它类似于方法的局部变量。@xst,还有groovy转换@Field
,它可以更改脚本变量的范围:因此@Field
是ScriptXXX类上的私有def var
,除了@Field
在脚本中不允许使用私有def var
。