Groovy中的全局常量 [英] Global constants in Groovy

查看:336
本文介绍了Groovy中的全局常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常需要在脚本的顶部声明常量,这个脚本可以在脚本的任何其他地方引用。在Groovy中,似乎如果你使用final来声明一个常量,那么它在子范围中是不可访问的。这个非常基本和普遍的要求的解决方案是什么?我现在所使用的解决方法是创建一个未绑定的变量,但这不是一个常量,也不是优雅的。

解决方案

Groovy doesn真的没有全球范围。当你有一个没有声明类的groovy脚本时,它会隐含在脚本的名字中。因此,顶级作用域的最终变量实际上只是隐式类的字段。例如:

  // foo.groovy 
final MYCONSTANT =foobar
println MYCONSTANT
$ b $ class Helper {
def hello(){println MYCONSTANT} //不会工作
}
new Helper()。hello()

或多或少相当于:

  class foo {
def run(){
final MYCONSTANT =foobar
println MYCONSTANT
new Helper()。hello()
}
$ main(args){
new foo()。run()
}
}

类助手{
def hello() {println MYCONSTANT} //不会工作
}

很容易理解为什么不工作扩大了。一个简单的解决方法就是在一个叫做全局变量的虚拟类中声明你的全局变量。常量,然后对它做一个静态导入。它甚至可以在一个脚本中运行。例子:

$ p $ import static Constants。*

类常量{
static final MYCONSTANT = foobar
}

println MYCONSTANT

类助手{
def hello(){println MYCONSTANT} //正常工作!

new Helper()。hello()

编辑: p>

另外,脚本有点特殊。如果你声明一个没有 def 的变量或者任何修饰符如 final ,(即使用它),它会进入脚本范围的绑定。所以在这种情况下:

  CONSTANT =foobar
printlnfoobar





CONSTANT在脚本范围内绑定,但是在:

  final CONSTANT =foobar
printlnfoobar

CONSTANT是脚本的 run()方法中的局部变量。关于这方面的更多信息可以在 https://web-beta.archive.org/web/20150108090004/http://groovy.codehaus.org/Scoping+and+the+Semantics+of+%22def%22


It is often desired to declare constants at the top of a script that can be referenced anywhere else in the script. In Groovy, it seems that if you declare a constant using final then it isnot accessible in child scopes. What is the solution for this very basic and common requirement? The workaround I have right now is to create an unbound variable but this is not a constant and is not elegant.

解决方案

Groovy doesn't really have a global scope. When you have a groovy script that doesn't declare a class, it implicitly gets stuck in a class with the name of the script. So final variables at the top-level scope are really just fields of the implicit class. For example:

// foo.groovy
final MYCONSTANT = "foobar"
println MYCONSTANT

class Helper {
    def hello() { println MYCONSTANT }  // won't work
}
new Helper().hello()

Is more or less equivalent to:

class foo {
    def run() {
        final MYCONSTANT = "foobar"
        println MYCONSTANT
        new Helper().hello()
    }
    static main(args) {
        new foo().run()
    }
}

class Helper {
    def hello() { println MYCONSTANT }  // won't work
}

It's easy to see why it doesn't work expanded out. An easy work around is to declare your "globals" in a dummy class called e.g. Constants, and then just do a static import on it. It even works all in a single script. Example:

import static Constants.*

class Constants {
    static final MYCONSTANT = "foobar"
}

println MYCONSTANT

class Helper {
    def hello() { println MYCONSTANT } // works!
}
new Helper().hello()

EDIT:

Also, scripts are bit of a special case. If you declare a variable without def or any modifiers such as final, (i.e. just use it) it goes into a script-wide binding. So in this case:

CONSTANT = "foobar"
println "foobar"

CONSTANT is in the script-wide binding, but in:

final CONSTANT = "foobar"
println "foobar"

CONSTANT is a local variable in the script's run() method. More information on this can be found at https://web-beta.archive.org/web/20150108090004/http://groovy.codehaus.org/Scoping+and+the+Semantics+of+%22def%22

这篇关于Groovy中的全局常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆