Groovy 全局变量时未声明的变量

Groovy 全局变量时未声明的变量,groovy,jira,Groovy,Jira,我是groovy的新手,我正在尝试制作我的第一个jira脚本。 下面的代码告诉我变量“finalMessage”是未声明的。它似乎与它是全局变量有关。 我做错了什么 finalMessage = "" def mainMethod() { logMessage "hello groovy" return finalMessage } def logMessage(message){ finalMessage += message } 我不确定你为什么要这么做,但是,我想我可

我是groovy的新手,我正在尝试制作我的第一个jira脚本。 下面的代码告诉我变量“finalMessage”是未声明的。它似乎与它是全局变量有关。 我做错了什么

finalMessage = ""

def mainMethod() {
  logMessage "hello groovy"
  return finalMessage
}

def logMessage(message){
   finalMessage += message
 }

我不确定你为什么要这么做,但是,我想我可能已经正确地解释了这一点。相反,您可以使用一个数组&然后在脚本末尾加入它以获得所需的内容。比如说

// This now becomes an array
finalMessage = []

// this function just calls the log message function? Not sure why you have this extra function
def mainMethod() {
  logMessage "hello groovy"
}


def logMessage(message){
    // instead of trying to change a global value, now that it's an array, you can push to the array
   finalMessage.push(message)
 }

 // here we call the above "mainMethod" in order to execute the logMessage function with the message "hello groovy"
 mainMethod()

     // this is the final message array now joined with spaces
  def joinedFinalMessage = finalMessage.join(' ')

// Print the final message 
joinedFinalMessage

请提供更多详情。是管道吗?另外,请显示代码失败的行。