Groovy范围 - 如何访问方法中的脚本变量 [英] Groovy scope - how to access script variable in a method
问题描述
我有一个关于Groovy中的范围规则的问题。在下面的代码片段中,我有三个变量, a
具有本地范围, b
具有脚本范围, c
应该使用 @Field
注释来获取脚本范围。
#!/ usr / bin / groovy
导入groovy.transform.Field;
// println org.codehaus.groovy.runtime.InvokerHelper.getVersion()
def a = 42;
b =Tea
@Field def c =Cheese
void func()
{
// println a // MissingPropertyException
println b //打印Tea
println c //用groovy打印Cheese2.1.2,用groovy打印MissingPropertyException 1.8.6
}
class Main
{
def method()
{
// println a // MissingPropertyException
// println b // MissingPropertyException
// println c // MissingPropertyException都与1.8.6。和2.1.2
}
}
func();
new Main()。method();
在指示的行上得到 MissingPropertyException
与评论。预计 a
出现异常,因为该变量具有局部范围。但是我期望 b
可以在 method()
中访问 - 事实并非如此。
@Field
在groovy 1.8.6中没有做任何事情,虽然在升级之后它可以工作,所以我猜这是一个老bug。尽管如此, c
在任何版本的 method()
中都不可访问。
所以我的问题是:
- 为什么我不能访问用
@Field
内
方法()
? - 如何在
method()
?
$中引用脚本变量当你在groovy脚本中有一个 class
声明之外的方法或语句时,隐式类被创建。回答你的问题:
$ b
-
在你的例子中,
func()
可以访问c
字段,因为它们都是隐式类的成员。Main
类不是,所以它不能。 -
您需要将引用传递给脚本变量为
method()
。一种方法是传递隐式定义的绑定
对象,通过它可以访问所有脚本范围变量。
示例:
#!/ usr / bin / groovy
导入groovy.transform.Field;
// println org.codehaus.groovy.runtime.InvokerHelper.getVersion()
def a = 42;
b =Tea
@Field def c =Cheese
void func()
{
// println a // MissingPropertyException
println b //打印Tea
println c //用groovy打印Cheese2.1.2,用groovy打印MissingPropertyException 1.8.6
}
class Main
{
def scriptObject
def binding
def method()
{
// println a // MissingPropertyException
println binding.b
println scriptObject.c
}
}
func();
new Main(scriptObject:this,binding:binding).method();
I have a question about scoping rules in Groovy. In the following snippet, I have three variables, a
has local scope, b
has script scope, and c
should get script scope as well using the @Field
annotation.
#!/usr/bin/groovy
import groovy.transform.Field;
//println org.codehaus.groovy.runtime.InvokerHelper.getVersion()
def a = 42;
b = "Tea"
@Field def c = "Cheese"
void func()
{
// println a // MissingPropertyException
println b // prints "Tea"
println c // prints "Cheese" with groovy 2.1.2, MissingPropertyException with groovy 1.8.6
}
class Main
{
def method()
{
// println a // MissingPropertyException
// println b // MissingPropertyException
// println c // MissingPropertyException with both 1.8.6. and 2.1.2
}
}
func();
new Main().method();
I get MissingPropertyException
s on the lines indicated with comments. The exceptions on a
are expected, as that variable has local scope. But I would expect b
to be accessible inside method()
- it isn't.
@Field
doesn't do anything in groovy 1.8.6, although after upgrading it works, so I guess that is an old bug. Nevertheless, c
is inaccessible inside method()
with either version.
So my questions are:
- Why can't I access a variable annotated with
@Field
insidemethod()
? - How can I refer to a script variable inside
method()
?
When you have methods or statements outside of a class
declaration in a groovy script, an implicit class is created. To answer your questions:
In your example,
func()
can access the fieldc
because they are both members of the implicit class. TheMain
class is not, so it can't.You need to pass a reference to the script variable to
method()
. One way is to pass the implicitly definedbinding
object, through which you can access all the script scope variables.
Example:
#!/usr/bin/groovy
import groovy.transform.Field;
//println org.codehaus.groovy.runtime.InvokerHelper.getVersion()
def a = 42;
b = "Tea"
@Field def c = "Cheese"
void func()
{
// println a // MissingPropertyException
println b // prints "Tea"
println c // prints "Cheese" with groovy 2.1.2, MissingPropertyException with groovy 1.8.6
}
class Main
{
def scriptObject
def binding
def method()
{
// println a // MissingPropertyException
println binding.b
println scriptObject.c
}
}
func();
new Main(scriptObject: this, binding: binding).method();
这篇关于Groovy范围 - 如何访问方法中的脚本变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!