Advertisement
fatimacasau

closures

Feb 5th, 2012
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 0.67 KB | None | 0 0
  1. // closure sin argumentos
  2. def hola = {println "hola"}
  3. hola()
  4.  
  5. // closure con un sólo argumento
  6. def saludo = {println "hola $it"}
  7. saludo("Fátima")
  8.  
  9. // closure con dos argumentos
  10. def printSum = { a, b -> println a+b }
  11. printSum( 5, 7 )
  12.  
  13. // closure con variables libres
  14. def a = 10
  15. def sum10 = { it + a}
  16. println sum10(5)
  17.  
  18. // this, owner & delegate
  19.  
  20. class Class1 {  
  21.     def closure = {    
  22.         println this.class.name    
  23.         println delegate.class.name    
  24.         def nestedClos = {      
  25.             println owner.class.name    
  26.         }    
  27.         nestedClos()  
  28.     }
  29. }  
  30. def clos = new Class1().closure
  31. clos.delegate = this
  32. clos()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement