加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-14 16:36:38 所属栏目:大数据 来源:网络整理
导读:在Groovy shell中,您可以输入命令,例如 def x = 1 并运行它们。该行回来了: groovy:000 def x = 1def x = 1=== 1groovy:000 现在,如果我输入: println(x) 我明白了: groovy:000 println(x)println(x)Unknown property: xgroovy:000 所以似乎控制台和sh
在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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读