基础语法

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

/**
* @author CZM
* @create 2021/5/29
* 参考链接
* @link https://blog.csdn.net/kmyhy/article/details/4200563
* @link https://blog.csdn.net/yanbober/article/details/49047515
* @link https://blog.csdn.net/zhaoyanjun6/article/details/70313790
*/
test = new LearnGroovy(["name":"小明", "sno":1111])
//test = new LearnGroovy("name":"小明", "sno":1111)
test.method()
println test.name
println test.@name //忽略getter方法,直接获取字段值
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()

//支持两种集合 collection(List(默认ArrayList)) 和map(默认LinkedHashMap)
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"}
//it是一个关键字,代表每一个遍历元素
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)


//int total =0
//add = { ... list -> {
// list.each{total += it}
// return add
//}}
//
//add.call(1,2).call(2,3,4)
//println total

//枚举
enum Day{
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY
}

def today = Day.SATURDAY
switch (today) {
//Saturday or Sunday
case [Day.SATURDAY, Day.SUNDAY]:
println "Weekends"
break
//a day between Monday and Friday
case Day.MONDAY..Day.FRIDAY:
println "work day"
break
default:
println "Are you sure this is a valid day?"

}

//Groovy所有的对象都有一个元类metaClass,我们可以通过metaClass属性访问该元类。通过元类,可以为这个对象增加方法
String msg = "abcdefg"
msg.metaClass.up = {delegate.toUpperCase()}
println msg.up()
//打印所有属性以及方法
msg.metaClass.properties.each { println it.name}
//msg.metaClass.methods.each { println it.name}

//assert msg == 1
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'

//GString使用闭包有延时特性
def number = 1
def eagerGString = "value == ${number}" //number在GString创建后就被替换
def lazyGString = "value == ${ -> number }"
assert eagerGString == "value == 1"
assert lazyGString == "value == 1"
number = 2
assert eagerGString == "value == 1" //eagerGString值不变
assert lazyGString == "value == 2" //lazyGString已经改为"value == 2"


//GString的与String值相同,hashcode也不同,尽量避免使用GString作map的key
key = "key"
println "${key}" == "key" //true
println "${key}".hashCode() == "key".hashCode() //false


// *.展开运算符,一个集合使用展开运算符可以得到一个元素为原集合各个元素执行后面指定方法所得值的集合
groovylist = [
new LearnGroovy(name:"小明"),
null,
new LearnGroovy(name:"小红"),
new LearnGroovy(name:"小丽")
]
println groovylist*.name //[小明, null, 小红, 小丽]
assert null*.name == null

参考链接

参考链接

参考链接

END