gpt4 book ai didi

grails - Groovy控制台无法 “remember”任何变量-总是说 “unknown property”

转载 作者:行者123 更新时间:2023-12-03 10:12:35 27 4
gpt4 key购买 nike

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

foo = "bar"

But, this will evaluate a local variable and will not be saved to the shell’s environment:

def foo = "bar"


您可以通过启用 interpreterMode来更改此行为
groovy:000> := interpreterMode
groovy:000> def x = 42
===> 42
groovy:000> x
===> 42
groovy:000>

关于grails - Groovy控制台无法 “remember”任何变量-总是说 “unknown property”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26716995/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com