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

前端 未结 8 974
我寻月下人不归
我寻月下人不归 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:37

    def sum = 0
    
    // This method stores a value in a global variable.
    def add =
    { 
        input1 , input2 ->
        sum = input1 + input2;
    }
    
    // This method uses stored value.
    def multiplySum =   
    {
        input1 ->
            return sum*input1;
    }
    
    add(1,2);
    multiplySum(10);
    

提交回复
热议问题