特征:
1.概念:
特征是语言的结构构造
(1)行为的组成。
(3)与静态类型检查/编译的兼容性
(4)它们可以被看作是承载默认实现和状态的接口。
void DisplayMarks() {
println("Display Marks");
}
}
static void main(String[] args) {
def clos = {println "Hello World"};
clos.call();
}
}
clos.call("World");
def clos = {param -> println "${str1} ${param}"}
clos.call("World");
// We are now changing the value of the String str1 which is referenced in the closure
str1 = "Welcome";
clos.call("World");
def static Display(clo) {
// This time the $param parameter gets replaced by the string "Inner"
clo.call("Inner");
}
static void main(String[] args) {
def str1 = "Hello";
def clos = { param -> println "${str1} ${param}" }
clos.call("World");
// We are now changing the value of the String str1 which is referenced in the closure
str1 = "Welcome";
clos.call("World");
// Passing our closure to a method
Example.Display(clos);
}
}
def value;
value = lst.find {element -> element > 2}
println(value);
2)findAll() 它找到接收对象中与闭合条件匹配的所有值。
def value;
value = lst.findAll{element -> element > 2}
value.each {println it}
3)any() & every() 方法any迭代集合的每个元素,检查布尔谓词是否对至少一个元素有效。
def value;
// Is there any value above 2
value = lst.any{element -> element > 2}
println(value);
// Is there any value above 4
value = lst.any{element -> element > 4}
println(value);
4)collect() 该方法通过集合收集迭代,使用闭包作为变换器将每个元素转换为新值。
def newlst = [];
newlst = lst.collect {element -> return element * element}
println(newlst);