Groovy 特征,闭包
特征:
1.概念:
特征是语言的结构构造
(1)行为的组成。
(2)接口的运行时实现。
(3)与静态类型检查/编译的兼容性
(4)它们可以被看作是承载默认实现和状态的接口。
(3)与静态类型检查/编译的兼容性
(4)它们可以被看作是承载默认实现和状态的接口。
2.关键修饰字:trait。
3.例子
trait Marks {
void DisplayMarks() {
println("Display Marks");
}
}
void DisplayMarks() {
println("Display Marks");
}
}
4.总结:
(1)可以使用implement关键字以类似于接口的方式实现trait。
(2)Traits可以实现接口,在这种情况下,使用implements关键字声明接口。
(3)特征可以定义属性。
(4)特征可以用于以受控的方式实现多重继承,避免钻石问题。比如我们定义了两个特征 - Marks和Total。我们的Student类实现了两个特征。由于学生类扩展了这两个特征,它能够访问这两种方法 - DisplayMarks和DisplayTotal。
(5)特征可能扩展另一个特征,在这种情况下,必须使用extends关键字。
闭包:
1.概念:闭包是一个短的匿名代码块。它通常跨越几行代码。一个方法甚至可以将代码块作为参数。它们是匿名的。
2.例子:
class Example {
static void main(String[] args) {
def clos = {println "Hello World"};
clos.call();
}
}
static void main(String[] args) {
def clos = {println "Hello World"};
clos.call();
}
}
总结:在上面的例子中,代码行 - {println“Hello World”}被称为闭包。此标识符引用的代码块可以使用call语句执行。
3.闭包中的形式参数:闭包也可以包含形式参数,以使它们更有用,就像Groovy中的方法一样。
例子:
def clos = {param->println "Hello ${param}"};
clos.call("World");
clos.call("World");
总结:在上面的代码示例中,注意使用$ {param},这导致closure接受一个参数。当通过clos.call语句调用闭包时,我们现在可以选择将一个参数传递给闭包。
4.闭包和变量:更正式地,闭包可以在定义闭包时引用变量。
例子:
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");
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");
总结:在上面的例子中,除了向闭包传递参数之外,我们还定义了一个名为str1的变量。闭包也接受变量和参数。
5.在方法中使用闭包:闭包也可以用作方法的参数。在Groovy中,很多用于数据类型(例如列表和集合)的内置方法都有闭包作为参数类型。
例子:
class Example {
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 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);
}
}
总结:1.定义一个名为Display的静态方法,它将闭包作为参数。2.我们在我们的main方法中定义一个闭包,并将它作为一个参数传递给我们的Display方法。
6.集合和字符串中的闭包
(1)使用闭包和列表:def lst = [11, 12, 13, 14]; lst.each {println it}
(2)使用映射闭包:def mp = ["TopicName" : "Maps", "TopicDescription" : "Methods in Maps"] ; mp.each {println it} ; mp.each {println "${it.key} maps to: ${it.value}"}
(3)闭包使用的方法:
1)find() find方法查找集合中与某个条件匹配的第一个值。
例子:
def lst = [1,2,3,4];
def value;
value = lst.find {element -> element > 2}
println(value);
2)findAll() 它找到接收对象中与闭合条件匹配的所有值。
def value;
value = lst.find {element -> element > 2}
println(value);
2)findAll() 它找到接收对象中与闭合条件匹配的所有值。
例子:
def lst = [1,2,3,4];
def value;
value = lst.findAll{element -> element > 2}
value.each {println it}
3)any() & every() 方法any迭代集合的每个元素,检查布尔谓词是否对至少一个元素有效。
def value;
value = lst.findAll{element -> element > 2}
value.each {println it}
3)any() & every() 方法any迭代集合的每个元素,检查布尔谓词是否对至少一个元素有效。
例子:
def lst = [1,2,3,4];
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);
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);
结果:
true
false
4)collect() 该方法通过集合收集迭代,使用闭包作为变换器将每个元素转换为新值。
4)collect() 该方法通过集合收集迭代,使用闭包作为变换器将每个元素转换为新值。
例子:
def lst = [1,2,3,4];
def newlst = [];
newlst = lst.collect {element -> return element * element}
println(newlst);
def newlst = [];
newlst = lst.collect {element -> return element * element}
println(newlst);

浙公网安备 33010602011771号