如题:
我们对Groovy闭包的理解不应该仅限于each等object的自带闭包,使用闭包可以让递归操作更加方便灵活. groovy官网上的闭包例子Fibonacci并不常见, 现在提供一个比较常见的递归打印文件功能.
- def tree
- tree = { file, arg ->
- println "${'| ' * arg} - ${file.getName()}\\"
- file.eachFile() { f ->
- if(f.directory){
- tree(f,(arg+1))
- }else{
- println "${'| ' * (arg+1)} - ${f.getName()}"
- }
- }
- }
- tree(new File('.'),0)
转载于:https://blog.51cto.com/agilemaster/1177072