Here is the example, to call methods from the Closure, at runTime
class MethodCallsInClosure {
def call1(){
println "Call 1";
}
def call2(){
println "Call 2";
}
//A Closure
def test(Closure c){
c.delegate = this;
c();
}
static void main(args){
MethodCallsInClosure m = new MethodCallsInClosure();
//invoking calls inside the Closure and pass to the methods
m.test{m.call1(); m.call2()}
m.test1{m.call1(); m.call2()}
}
//another way of Closure
def test1 = {
it.delegate = this;
it.doCall();
it();
}
}
class MethodCallsInClosure {
def call1(){
println "Call 1";
}
def call2(){
println "Call 2";
}
//A Closure
def test(Closure c){
c.delegate = this;
c();
}
static void main(args){
MethodCallsInClosure m = new MethodCallsInClosure();
//invoking calls inside the Closure and pass to the methods
m.test{m.call1(); m.call2()}
m.test1{m.call1(); m.call2()}
}
//another way of Closure
def test1 = {
it.delegate = this;
it.doCall();
it();
}
}
prints :
Call 1
Call 2
Call 1
Call 2
Call 1
Call 2
No comments:
Post a Comment