variablescopingbug.groovy

来自「大名鼎鼎的java动态脚本语言。已经通过了sun的认证」· GROOVY 代码 · 共 43 行

GROOVY
43
字号
import org.codehaus.groovy.classgen.TestSupport/** * @version $Revision: 1.8 $ */class VariableScopingBug extends TestSupport {        void testBug() {        // undeclared variable x        shouldFail {            def shell = new GroovyShell()            shell.evaluate("""                class SomeTest {                    void run() {                        for (z in 0..2) {                            def x = [1, 2, 3]                        }                        for (t in 0..3) {                            for (y in x) {                                println x                            }                        }                    }               }               new SomeTest().run()""")           }    }    void testVariableReuse() {        def shell = new GroovyShell()        shell.evaluate("""            for (z in 0..2) {                def x = [1, 2, 3]            }            for (t in 0..3) {                def x = 123                println x            }""")    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?