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.
使用和使用有什么区别
teams.each(){ team-> //iterate through each team }
和
teams.each { team-> //iterate through each team }
请注意,在第一个之后和关闭之前都有一个括号。我们正在对我们的应用程序进行一些负载测试,并注意到当我们使用第一个时,有一些线程在等待这个调用。但在第二种情况下,一切顺利。
我很想知道这里有什么区别。
没什么,它们是一样的。在 Groovy 中,如果函数的最后一个参数是闭包,那么它可以出现在大括号之外,例如:
[1,2,3].inject( 0 ) { acc, it -> acc + it }
[1,2,3].inject( 0, { acc, it-> acc + it } )
是一样的东西。编写 groovy 的常用方法是在您的示例中省略大括号,或者在inject上面的示例中将闭包放在大括号之外
inject