我有一个这样的方法
def uploadFile(String fileName, boolean performCleanup = false) {
//upload file
if (performCleanup == true) {
//delete local file
}
}
我用两种方式调用这个方法:
uploadFile("/some/path/to/file.txt")
uploadFile("/some/path/to/file.txt", true)
在第二种情况下,为了更好的可读性,我想传递一个变量名称来表明传递 true
的意义。
类似于,uploadFile("/some/path/to/file.txt", PerformCleanup: true")
这可能吗?
最佳答案
通过引入和额外的闭包,您可以创建一个可读性良好的微型 DSL:
def performCleanup = {
println "cleanup $it" //delete local file
}
def upload(fileName) {
println "upload file $fileName" //upload file
[then: { action ->
action(fileName)
}]
}
upload "path"
upload "path" then performCleanup
关于groovy - 如何将 bool 值传递给具有变量名的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38603039/