grails – Groovy控制台无法“记住”任何变量 – 总是说“未知属性”

在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>

相关文章

背景:    8月29日,凌晨4点左右,某服务告警,其中一个...
https://support.smartbear.comeadyapi/docs/soapui/steps/g...
有几个选项可用于执行自定义JMeter脚本并扩展基线JMeter功能...
Scala和Java为静态语言,Groovy为动态语言Scala:函数式编程,...
出处:https://www.jianshu.com/p/ce6f8a1f66f4一、一些内部...
在运行groovy的junit方法时,报了这个错误:java.lang.Excep...