Thursday, November 17, 2011

Class, @Delegate

class A { String doIt() {println 'do A'} }

class B { String doIt() {println 'do B'} }

def pc = { println ( (it == A ? '' : 'x: ') + it.getName()) }

pc(A)
pc(B) //fail
pc(A1) //fail

def a = new A()
pc(a.getClass())


class A1 extends A { 
    @Delegate B b = new B()
    @Delegate A a = new A() // red herring
}

class C { 
    @Delegate B b = new B()
    @Delegate A a = new A() // red herring
}


def a1 = new A1()
a1.doIt() // do A

def c = new C()
c.doIt() // do B

No comments:

Post a Comment