Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
String someMethod(def columnOne, int number, columnName){ return columnOne + '-' + number + "${ -> (columnName == '') ?: '-' + columnName}" }
当我用以下方式调用它时:
someMethod('one', 2, '')
我得到以下结果:
one-2true
为什么闭包返回true而不是''or-somehting
true
''
-somehting
你不需要这里的猫王,而是普通的三元,像这样:
(columnName ? '-'+columnName : '')
Elvis 是x ?: y=>的缩写, thenx ? x : y的结果columnName==''就是 is true,这将打印出来。
x ?: y
x ? x : y
columnName==''
充分利用 groovy 的 GString。字符串连接很昂贵。
return "$columnOne-$number${columnName ? '-' + columnName : ''}"