If you look very carefully at the picture included, you will notice that you can refactor Groovy code using the Eclipse IDE and convert a method to a closure and vice versa. So, what exactly is a closure again and how is it different than a method? Can someone give a good example of using a closure as well as why it's a useful feature? Anonymous inner classes weren't good enough?
相关问题
- Grails External Configuration. Can't access to
- How to include FFI in OS X?
- Can I override cast operator in Groovy?
- Use Groovy to Sort XML File
- C++ Lambda - error: no matching function for call
相关文章
- Using Spring Dynamic Languages Support from Groovy
- Will java allow to use functional interfaces as me
- Grails: How to make everything I create Upper Case
- How to scope closure's variables in CF10?
- How to define array of closures in Swift?
- Groovy Java 9 modules support
- Groovy: What's wrong with this “Hello World” p
- Create WS security headers for REST web service in
Also, as Closures are first class objects, they can be passed around, returned and manipulated. Consider:
Closure is a Closure class instance, that implements Call logic. It may be passed as argument or assigned to a variable. It also has some logic concerned with scope variable accessing and delegating calls.
Methods are normal Java methods. Nothing special.
And yes, anonymous inner classes have a lot of boilerplate code to perform simple actions.
Compare:
vs
There is a related question on SO Groovy : Closures or Methods and the following link(s) to the user guide containing a lot of useful information.