列表允许您保存特定的值(唯一或不唯一).设置可让您保留特定值(所有值都是唯一的).地图允许您具有键,值对(键必须是唯一的).在此处阅读更多常规列表,常规地图 I want define a variable in groovy with where the variable name is passed by another variable.Something like.def runExtFunc(varName){ def varName // => def abc varName = load 'someFile.groovy' // abc = load 'someFile.groovy' varName."$varName"() // -> abc.abc() (run function defined in File)} [...]runExtFunc('abc') // -> abc.abc() (Function abc defined in File) [...]runExtFunc('xyz') // -> xyz.xyz() (Function xyz defined in File) [...]Sadly def varName defines the variable varName and not abc. When I call runExtFunc twice an error occoures bacause varName is already defined.I also tried def runExtFunc(varName){ def "${varName}" // => def abc [...] "${varName}" = load 'someFile.groovy' [...]}which doesn't work either.Any suggestions? 解决方案 This is the wrong approach. Normally you would use List, Map or Set data structures, which allow you to save a collection and access specific elements in the collection.List allows you to hold specific values (unique or non-unique). Set allows you to hold specific values (all unique). Map allows you to have Key, Value pairs (Key must be unique) .Read more heregroovy list, groovy map 这篇关于Groovy-定义变量,其中变量名由另一个变量传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-17 18:07