Advertisement
dmahapatro

delegate

May 21st, 2013
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 0.70 KB | None | 0 0
  1. class Main {
  2.     public static void main(String[] args) {
  3.         Main m = new Main()
  4.     }
  5.  
  6.     Main() {
  7.         String[] a = ["Hello world!"]
  8.         Box b = new TextBox(a)
  9.         b.click = {
  10.             content.add("You clicked this box!")
  11.         }
  12.         b.onClick()
  13.        
  14.         println b.content
  15.     }
  16.    
  17.     class Box {
  18.         Closure click
  19.    
  20.         Box () {
  21.             click = {}
  22.         }
  23.    
  24.         void onClick() {
  25.             click.delegate = this
  26.             click()
  27.         }
  28.     }
  29.    
  30.     class TextBox extends Box {
  31.         List<String> content    
  32.    
  33.         TextBox (String[] a) {
  34.             super()
  35.             content = a
  36.         }
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement