`
romejiang
  • 浏览: 95335 次
  • 性别: Icon_minigender_1
  • 来自: ...
社区版块
存档分类
最新评论

Groovy 闭包深入浅出

阅读更多

闭包概念

Groovy语言中闭包(closure)是一个非常重要的概念,而且深入的理解了闭包对充分用好Groovy有很大帮助。对闭包比较书面的一种解释“闭包是可以用作函数参数和方法参数的代码块”。其实Groovy的闭包更象是一个“代码块”或者方法指针,代码在某处被定义然后在其后的调用处执行。

现在支持闭包的语言有 Scheme、Common Lisp、Smalltalk、Groovy、JavaScript、Perl,Ruby 和 Python 。

闭包基础
来看一个例子:

def square = {it * it} // 定义一个叫square的闭包。it是默认的 参数名称

assert 4 == square(2) // 使用闭包

assert [1,4,9] == [1,2,3].collect(square) // 使用闭包


闭包类似Java的内类,区别是闭包只有单一的方法可以调用,但可以有任意的参数,闭包用“{}”括起,“->”前面是参数,后面是处理语句,可以直接调用,也可以使用call调用。不管那种调用,最后groovy编译器都会把编译成对doCall方法的调用,这是groovy对闭包的一个隐藏方法。

def closure = { param -> println("hello ${param}") }
closure.call("world!")
 
def closure = { greeting, name -> println(greeting + name) }
closure.call("hello ", "world!")

第一个例子演示了在字符串内使用参数的形式:${param}
第二个例子演示了多参数形式:用“,”分隔参数

如果你的参数少于2个,那么它可以直接忽略掉。如果只有一个参数,可以不写,而使用缺省的参数“it”,如下面的例子:

closure = { println "hello " + it }
closure.call("world!")

把闭包当作变量返回的例子:
def localMethod() {
  def localVariable = new java.util.Date()
  return { println localVariable }
}
 
def clos = localMethod()
 
println "Executing the closure:"
clos() 

 

Groovy闭包中几个隐含变量

it:默认的参数名,调用是如果没有传参数,it为null
this : 跟Java一样,是定义闭包所在类的一个引用,不管有多少层闭包嵌套,this指向的都是最上层的类。
owner : 封闭闭包的对象(如果只有一层闭包就是this,如果有多层闭包嵌套就是含有此闭包的上层闭包)
delegate :缺省值是owner,但是可以改变,后面详说。
看例子:

class Class1 {
  def closure = {
    println " ============================== "
    println "this = "+ this.class.name
    println "owner = " + owner.class.name
    println "delegate = " + delegate.class.name
    def nestedClos = {
        println " ============================== "
        println "this = "+ this.class.name
        println "owner = " + owner.class.name
        println "delegate = " + delegate.class.name
      def thirdClos = {
            println " ============================== "
            println "this = "+ this.class.name
            println "owner = " + owner.class.name
            println "delegate = " + delegate.class.name
      }
      thirdClos() 
    }
    nestedClos()
  }
}
 
def clos = new Class1().closure
//clos.delegate = this
clos()

执行结果:
 ==============================
this = Class1
owner = Class1
delegate = Class1
 ==============================
this = Class1
owner = Class1$_closure1
delegate = Class1$_closure1
 ==============================
this = Class1
owner = Class1$_closure1_closure2
delegate = Class1$_closure1_closure2

闭包实现接口

前面说了用闭包实现类,或继承其他类。现在看看怎么实现接口。


interface Test
{
 def test()
}
 
 
def test = {
 println'ok'
} as Test
    
test.test()
如果接口只有一个方法需要实现,比如Comparator,Runnable等接口就可以直接实现方法后加上 as Runnable接口名就可以。

多方法接口,groovy用Map的方法实现。

interface MultiFuncTest
{
    def test1()
    def test2(str)
}
 
 
def impl = [test1:{println'test'},
        test2:{str -> println str}] as MultiFuncTest


impl.test1()
impl.test2('ok')


delegate委托的用法
delegate委托在是一种常用设计模式,但在java中实现相对比较繁琐,groovy直接在GroovyObject中已经实现了delegate模式,所以在groovy中应用delegate很方便。

class Dog{
    def play = {
      "wang wang!"
    }
    def childmind = {
        println       delegate.play();     
    }
}

class Cat {
    def play = {"mi mi !"}
}

def dog = new Dog()
def cat = new Cat()

dog.childmind()

dog.childmind.delegate  = cat;
dog.childmind()

上面的例子是狗爸爸让老猫帮忙照看他的狗儿子玩游戏。

闭包是Groovy一种核心技术,也是现在比较流行的动态语言的核心技术,所以全面稳定的掌握闭包是非常必要的。


参考:
http://blog.csdn.net/hivon/archive/2008/04/23/2318760.aspx  Groovy探索之闭包 三
http://www.chinagroovy.org/groovywiki/doku.php/wiki:user_guide:closures Groovy文档翻译,什么是闭包

 

6
0
分享到:
评论

相关推荐

    groovy(10)-闭包委托策略1

    groovy(10)-闭包委托策略 /*闭包的三个重要变量:this,owner,delegate区别在于:this代表闭包定义处最近的对象(不包含闭包),ow

    [Groovy入门]第五讲.将流程控制语句与方法重构为闭包

    [Groovy入门]第五讲.将流程控制语句与方法重构为闭包

    apache-groovy-sdk-2.4.11

    了解 Groovy 对 Java 语法的简化变形,学习 Groovy 的核心功能,例如本地集合、内置正则表达式和闭包。编写第一个 Groovy 类,然后学习如何使用 JUnit 轻松地进行测试。借助功能完善的 Groovy 开发环境和使用技能,...

    groovy入门实例代码详细(包括字符串,map,闭包等等)

    这个代码包是老师上课给我们的,感觉对于初学者来说非常实用,里面包含了基本语法,包括字符串,整数,小数,数组,闭包等待,让你快速入门

    Groovy入门经典.pdf

     Groovy提供类似于Java的语法结构,本地化支持映射和列表、方法、类,闭包和构造器等结构。由于具有动态弱类型,以及无缝访问JavaAPI等特性,Groovy语言非常适合子开发中小型规模的应用程序。  相对于Java语言,...

    Groovy v2.4.13官方版

    使用该种语言不必编写过多的代码,同时又具有闭包和动态语言中的其他特性。  Groovy是JVM的一个替代语言(替代是指可以用 Groovy 在Java平台上进行Java 编程),使用方式基本与使用 Java代码的方式相同,该语言特别...

    groovy-3.0.9-API文档-中文版.zip

    赠送jar包:groovy-3.0.9.jar; 赠送原API文档:groovy-3.0.9-javadoc.jar; 赠送源代码:groovy-3.0.9-sources.jar; 赠送Maven依赖信息文件:groovy-3.0.9.pom; 包含翻译后的API文档:groovy-3.0.9-javadoc-API...

    apache-groovy-3.0.8.zip apache官网的groovy3.0.8版本

    apache-groovy-3.0.8.zip apache官网的groovy3.0.8版本,希望大家多多下载,apache-groovy-3.0.8.zip apache官网的groovy3.0.8版本,希望大家多多下载,apache-groovy-3.0.8.zip apache官网的groovy3.0.8版本,希望...

    groovy入门经典,groovyeclipse 插件

    groovy入门经典,groovyeclipse 插件

    [Groovy] Making Java Groovy 英文版

    Making Java Groovy is a practical handbook for developers who want to blend Groovy into their day to day work with Java It starts by introducing the key differences between Java and Groovy and how you...

    apache-groovy-sdk-3.0.9.zip

    了解 Groovy 对 Java 语法的简化变形,学习 Groovy 的核心功能,例如本地集合、内置正则表达式和闭包。编写第一个 Groovy 类,然后学习如何使用 JUnit 轻松地进行测试。借助功能完善的 Groovy 开发环境和使用技能,...

    Java调用Groovy,实时动态加载数据库groovy脚本

    Java调用Groovy,实时动态加载数据库groovy脚本,java读取mongoDB的groovy脚本,加载实时运行,热部署

    Groovy轻松入门—搭建Groovy开发环境

    Groovy轻松入门—搭建Groovy开发环境 Groovy轻松入门—搭建Groovy开发环境

    Groovy入门经典

    Groovy提供类似于Java的语法结构,本地化支持映射和列表、方法、类,闭包和构造器等结构。由于具有动态弱类型,以及无缝访问JavaAPI等特性,Groovy语言非常适合子开发中小型规模的应用程序。, 相对于Java语言,...

    groovy-2.3.6-installer

    groovy-2.3.6-installer windows安装版本

    Groovy学习笔记 PDF版

    Groovy

    groovy-3.0.7.msi

    groovy

    精通 Groovy--下一代开发语言

    什么是 Groovy? Groovy 是 JVM 的一个替代语言 — 替代 是指可以用 Groovy 在 Java 平台上进行 Java 编程,使用方式基本与使用 Java 代码的方式相同。在编写新应用程序时,Groovy 代码能够与 Java 代码很好地结合,...

    Groovy.in.Action.2nd.Edition.1935182

    Groovy in Action, Second Edition is the undisputed definitive reference on the Groovy language. Written by core members of the Groovy language team, this book presents Groovy like no other can—from ...

Global site tag (gtag.js) - Google Analytics