在Groovy shell中,您可以输入命令,例如
def x = 1
并运行它们。该行回来了:
groovy:000> > def x = 1 def x = 1 ===> 1 groovy:000>
现在,如果我输入:
println(x)
我明白了:
groovy:000> > println(x) println(x) Unknown property: x groovy:000>
所以似乎控制台和shell都记得对象定义,这是正常的吗?
解决方法
这是Groovy shell中的标准行为,不是Grails shell特有的。你可能不想def变量。请参阅以下内容:
~ $ groovysh Groovy Shell (2.3.4,JVM: 1.7.0_45) Type ':help' or ':h' for help. ------------------------------------------------------------------------------- groovy:000> def x = 42 ===> 42 groovy:000> x Unknown property: x groovy:000> y = 2112 ===> 2112 groovy:000> y ===> 2112 groovy:000>
从http://beta.groovy-lang.org/groovysh.html起
1.3.4. Variables
Shell variables are all untyped (ie. no
def
or other type information).This will set a shell variable:
06001
But,this will evaluate a local variable and will not be saved to the shell’s environment:
06002
您可以通过启用interpreterMode来更改此行为
groovy:000> := interpreterMode groovy:000> def x = 42 ===> 42 groovy:000> x ===> 42 groovy:000>