Last active
July 13, 2020 11:31
-
-
Save songtianyi/21b1935f1c9fbb48cc7a5c5ca7f14ed8 to your computer and use it in GitHub Desktop.
groovy demos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GroovyTest { | |
// sum method | |
def static sum(n, closure) { | |
for(int i = 2; i <= n; i += 2) { | |
closure(i) | |
} | |
// return 2 | |
2 | |
} | |
def static paramsDemo(a, b, c, closure) { | |
closure(a + b + c, a + b) | |
} | |
public static void main(String []args){ | |
println "java in groovy!" | |
for(int i = 0;i < 10;i++) { | |
System.out.print(i); | |
print i + "," | |
} | |
println "\nrange for"; | |
for (i in 0..2) { | |
print i | |
} | |
println "\nclosure" | |
0.upto(10){ | |
print "$it" | |
} | |
println "\nrepeat " | |
3.times { | |
print "index $it," | |
print "ho " | |
} | |
println "\nstep demo" | |
1.step(10, 5) { | |
print "$it," | |
} | |
println "\nsystem command invocation!" | |
print "java -version".execute().text; | |
println "\na new way to check null" | |
String a = null | |
a?.print() | |
println "\ncatch exception is not necessary!" | |
println "semicolon is not necessary!" | |
// evaluate expression in string | |
println "${1==0}" | |
def total = 1 | |
// 666 | |
println "${sum(10, {index -> println "$index";total *= index})}" | |
println total | |
sum(10){index -> total *= index} | |
println total | |
paramsDemo("hello ", "world", "!") { | |
cb1, cb2 -> println cb1 + cb2 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment