1

我是 Java 和 Groovy 的新手,这是一个关于groovysh逐步调试 groovy 代码的非常简单的问题。

$ groovysh
groovy:000> String str = "abcd"
===> abcd
groovy:000> println str
Unknown property: str

在所有其他语言的交互式调试 shell 中,我能够定义一个变量并在以下所有步骤中使用它。怎么做groovysh呢?

4

1 回答 1

2

正如groovysh 文档页面中定义的那样,默认情况下所有变量都是无类型的,因此使用def或特定类型标识符(如String)不起作用。在这种情况下,正确的语法就是str = "abcd".

$ groovysh
Groovy Shell (3.0.6, JVM: 11.0.6)
Type ':help' or ':h' for help.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
groovy:000> str = "abcd"
===> abcd
groovy:000> str
===> abcd
groovy:000> :S variables
Variables:
  str = abcd
  _ = abcd
===> [str:abcd, _:abcd]

正如您在上面的示例中所看到的,您可以使用该:S variables命令列出在 shell 会话中注册的所有变量。:h(您可以列出在 shell 窗口中执行的所有可用命令。)

但是,有一种方法可以打开类型化变量。这称为解释模式,可以使用:= interpretedMode以下示例中的命令激活它:

$ groovysh
Groovy Shell (3.0.6, JVM: 11.0.6)
Type ':help' or ':h' for help.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
groovy:000> str = "abcd"
===> abcd
groovy:000> str
===> abcd
groovy:000> :S variables
Variables:
  str = abcd
  _ = abcd
===> [str:abcd, _:abcd]
groovy:000> := interpreterMode
groovy:000> String str2 = "efgh"
===> efgh
groovy:000> str2
===> efgh
groovy:000> str == str2
===> false
groovy:000> :S variables
Variables:
  str = abcd
  _ = false
  groovysh_collected_boundvars = [str2:efgh]
  str2 = efgh
===> [str:abcd, _:false, groovysh_collected_boundvars:[str2:efgh], str2:efgh]

于 2020-11-24T17:33:26.380 回答