1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
| package com.groovy
test = new LearnGroovy(["name":"小明", "sno":1111])
test.method() println test.name println test.@name println test.sno println test.getSno()
test?.method()
if( test!= null) { test.method() }
Object test3 = test ? test : "空对象"
Object test2 = test ?: "空对象"
println test2.class
println "${test}".class def tt(){ println "Tt" } for (def i = 0; i < 3; i++) println test.class tt()
def collection = [1,"str",test]
collection[collection.size()] = "new1" collection << "new2" collection.add("new3")
println collection println collection[2] collection.remove test println collection[2]
println collection[-2] println collection.class
def map = ["key1": "1", key2 : 2] map.put(3, 3) map += ["new1":1, "new2" : 2] println map println map["key2"] println map.key2 println map.getClass() println "遍历" map.each {key,val -> println "$key:$val"}
map.each {println it} map.each {println it.getKey() +":"+ it.getValue()}
Map<String, String> map2 = new HashMap<>() println map2.getClass()
def set = [1,2,3] as HashSet println set.class
def closure = { param1,param2 -> { println "参数1:$param1, 参数2:${param2}" println "参数2类型:" +param2.getClass() }} closure("你好","aa") closure.call("你好2", new HashSet<Integer>())
int sum(int... var) { def total = 0
for (i in var)
total += i
return total }
println sum(1,4,5,6) println sum(1,7,8)
enum Day{ SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }
def today = Day.SATURDAY switch (today) { case [Day.SATURDAY, Day.SUNDAY]: println "Weekends" break case Day.MONDAY..Day.FRIDAY: println "work day" break default: println "Are you sure this is a valid day?"
}
String msg = "abcdefg" msg.metaClass.up = {delegate.toUpperCase()} println msg.up()
msg.metaClass.properties.each { println it.name}
println "###"
def sum() { int total =0 add = { ... list -> { list.each{ total += it} return add }} val = {total} } tt = sum() tt.add(1,2)(10) println tt.val()
def list = ['a','b','c']
list.each{println it} String printName(it){ println "自定义显示:$it" }
list.each(this.&printName)
def sParameterLessClosure = "1 + 2 == ${-> 3}" assert sParameterLessClosure == '1 + 2 == 3'
def sOneParamClosure = "1 + 2 == ${ w -> w << 3}" assert sOneParamClosure == '1 + 2 == 3'
def number = 1 def eagerGString = "value == ${number}" def lazyGString = "value == ${ -> number }" assert eagerGString == "value == 1" assert lazyGString == "value == 1" number = 2 assert eagerGString == "value == 1" assert lazyGString == "value == 2"
key = "key" println "${key}" == "key" println "${key}".hashCode() == "key".hashCode()
groovylist = [ new LearnGroovy(name:"小明"), null, new LearnGroovy(name:"小红"), new LearnGroovy(name:"小丽") ] println groovylist*.name assert null*.name == null
|