How do I create and access the global variables in Groovy?

前端 未结 8 973
我寻月下人不归
我寻月下人不归 2020-11-28 22:29

I need to store a value in a variable in one method and then I need to use that value from that variable in another method or closure. How can I share this value?

8条回答
  •  猫巷女王i
    2020-11-28 22:44

    Like all OO languages, Groovy has no concept of "global" by itself (unlike, say, BASIC, Python or Perl).

    If you have several methods that need to share the same variable, use a field:

    class Foo {
        def a;
    
        def foo() {
            a = 1;
        }
        def bar() {
            print a;
        }
    }
    

提交回复
热议问题