GROOVY 闭包

https://www.w3cschool.cn/groovy/groovy_closures.html

class Example{
    def static Display(clo){
        clo.call("Inner");
    }

    static void main(String[] args){
        // def clos = {print "Hello World!"};
        // clos.call();

        // def clos = {param ->println "Hello ${param}"};
        // clos.call("world!");

        // def clos = {println "hello ${it}"};
        // clos.call("world!");

        // def str1 = "hello";
        // def clos = {param -> println "${str1} ${param}"};
        // clos.call("groovy!");

        // str1 = "welcome";
        // clos.call("groovy!");

        // Example.Display(clos);
        
        // def lst = [12,13,14,15];
        // lst.each {println it};

        // def mp = ["name":"jack","age":30];
        // mp.each {println it};
        // mp.each {println "${it.key} maps to:${it.value}" };

        def lst = [1,2,3,4];
        lst.each {println it};
        println("The list will only display those numbers which are divisible by 2");
        lst.each {num -> if(num % 2 == 0) println num};
    }
}

 

posted @ 2025-07-22 11:30  山村放羊娃  阅读(6)  评论(0)    收藏  举报