`
scanfprintf123
  • 浏览: 79094 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Groovy闭包中的this,owner和delegate

阅读更多

要想了解Groovy闭包中的this,owner和delegate的含义,首先我们需要知道闭包能在哪些上下文中进行创建。

 

创建闭包的上下文

 

首先,闭包可以在方法体中创建(类的实例方法或者静态方法均可)

 

class Person{
	
	def static method(){
		def methodClosure={
			println "methodClosure this:"+this
			println "methodClosure owner:"+owner
			println "methodClosure delegate:"+delegate
		}
		methodClosure
	}
}

Person.method().call()
 

输出:

 

 

methodClosure this:class test.Person

methodClosure owner:class test.Person

methodClosure delegate:class test.Person

 

其次, 闭包可以在类中直接定义:

 

 

class Person{
	def static classClosure={
		println "classClosure this:"+this
		println "classClosure owner:"+owner
		println "classClosure delegate:"+delegate
	}

}

Person.classClosure.call()
 

输出:

 

 

classClosure this:class test.Person

classClosure owner:class test.Person

classClosure delegate:class test.Person

 

再者,闭包可以在groovy的script中直接定义,实际上也是在类中直接定义,同上,因为groovy的script实际上会被编译为Script.class

 

 

def scriptClosure={
	println "scriptClosure this:"+this
	println "scriptClosure owner:"+owner
	println "scriptClosure delegate:"+delegate
}

scriptClosure.call()

 

输出:

 

 

scriptClosure this:test.Script@a09e41

scriptClosure owner:test.Script@a09e41

scriptClosure delegate:test.Script@a09e41

 

最后, 闭包可以在闭包中定义:

 

 

def closure={
	def closureClosure={
		println "closureClosure this:"+this
		println "closureClosure owner:"+owner
		println "closureClosure delegate:"+delegate
	}
	closureClosure.call()
}

closure.call()

 

输出:

 

 

closureClosure this:test.Script@27cd63

closureClosure owner:test.Script$_run_closure2@746ad0

closureClosure delegate:test.Script$_run_closure2@746ad0

 

从上可以看到,闭包可以在四种上下文中进行定义,但是闭包中的this,owner 和 delegate的含义却各不相同。 

 

 

静态闭包和实例闭包

实际上,闭包根据其创建的上下文不同,还可以分为静态闭包和实例闭包,在这两种情况下,this,owner和delegate的含义也是不同的。

 

class ClosureTest{
	def static classClosure={
		println "classClosure this:"+this
		println "classClosure owner:"+owner
		println "classClosure delegate:"+delegate
	}
	
	def instanceClosure={
		println "instanceClosure this:"+this
		println "instanceClosure owner:"+owner
		println "instanceClosure delegate:"+delegate
	}
	
	def static classMethodClosure(){
		def classMethodClosure={
			println "classMethodClosure this:"+this
			println "classMethodClosure owner:"+owner
			println "classMethodClosure delegate:"+delegate
		}
		classMethodClosure.call()
	}
	
	def  instanceMethodClosure(){
		def instanceMethodClosure={
			println "instanceMethodClosure this:"+this
			println "instanceMethodClosure owner:"+owner
			println "instanceMethodClosure delegate:"+delegate
		}
		instanceMethodClosure.call()
	}
}

ClosureTest.classClosure()
new ClosureTest().instanceClosure()
ClosureTest.classMethodClosure()
new ClosureTest().instanceMethodClosure()

 

输出:

 

classClosure this:class test.ClosureTest
classClosure owner:class test.ClosureTest
classClosure delegate:class test.ClosureTest
instanceClosure this:test.ClosureTest@11d3226
instanceClosure owner:test.ClosureTest@11d3226
instanceClosure delegate:test.ClosureTest@11d3226
classMethodClosure this:class test.ClosureTest
classMethodClosure owner:class test.ClosureTest
classMethodClosure delegate:class test.ClosureTest
instanceMethodClosure this:test.ClosureTest@ecb3f1
instanceMethodClosure owner:test.ClosureTest@ecb3f1
instanceMethodClosure delegate:test.ClosureTest@ecb3f1
 

 

This在闭包中的含义

 

对于this来讲,它基本上保持了跟java中this一样的含义(在java的静态方法以及静态域中,this是没有任何含义的),在上面的闭包创建的4种上下文中,实际上可以了解为只有2种,一种是在普通的类中定义,如上面的Person类,一种是在groovy script中定义,实际上也是在类中定义,只不过这个是一个比较特殊的类而已(Groovy会将groovy script编译为Script.class), 所以,this在闭包中的含义指的是,表示定义该闭包的类的实例对象(实例闭包)或者类本身(静态闭包)。

 

 

 

Owner在闭包中的含义

对于owner来讲,它的含义基本上跟this的含义一样,只是除了一种情况,如果该闭包是在其他的闭包中定义的,那么owner是指向定义它的闭包对象。 如上面最后一种创建上下文:

 

closureClosure owner:test.Script$_run_closure2@746ad0
 

Delegate在闭包中的含义

对于delegate来讲,它的含义大多数情况下是跟owner的含义一样,除非它被显示的修改(通过Closure.setDelegate()方法进行修改)。

 

在上面的几种创建上下文中,可以看到,如果闭包的delegate没有被显示改动的话,那么delegate确实是同owner是一个含义。下面我们看看修改delegate的情况:

 

 

def scriptClosure={
	println "scriptClosure this:"+this
	println "scriptClosure owner:"+owner
	println "scriptClosure delegate:"+delegate
}
println "before setDelegate()"
scriptClosure.call()
scriptClosure.setDelegate ("abc")
println "after setDelegate()"
scriptClosure.call()
 

 

输出:

 

 

before setDelegate()
scriptClosure this:test.Script@67c1a6
scriptClosure owner:test.Script@67c1a6
scriptClosure delegate:test.Script@67c1a6
after setDelegate()
scriptClosure this:test.Script@67c1a6
scriptClosure owner:test.Script@67c1a6
scriptClosure delegate:abc

 

其中,delegate可以被设置为任意一个对象。

 

 

下面可以看下groovy中使用了setDelegate的应用:

 

 

String.metaClass.doTestClosure={
	println "doTestClosure this:"+this
	println "doTestClosure owner:"+owner
	println "doTestClosure delegate:"+delegate
}
"do test".doTestClosure()


Integer.metaClass.doTestClosure={
	println "doTestClosure this:"+this
	println "doTestClosure owner:"+owner
	println "doTestClosure delegate:"+delegate
}
1.doTestClosure()

 

输出:

 

 

doTestClosure this:test.Script@ffeef1
doTestClosure owner:test.Script@ffeef1
doTestClosure delegate:do test
doTestClosure this:test.Script@ffeef1
doTestClosure owner:test.Script@ffeef1
doTestClosure delegate:1

 

可以看到,在通过metaClass动态添加方法时,delegate都被动态的设置为了调用者的实例本身,如上面的"do test"字符窜,以及整数1. 

 

setDelegate还被广泛的使用于groovy builder的构建中。有兴趣的可以看下groovy中BuildSupport的实现。

 

 

分享到:
评论

相关推荐

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

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

    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;...人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。

    groovy和Java相互调用1

    Groovy 调用 Java 类groovy 调用 Java class 十分方便,只需要在类前导入该 Java 类,在 Groovy 代码中就可以无缝使用该

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

    赠送jar包:groovy-2.5.1.jar; 赠送原API文档:groovy-2.5.1-javadoc.jar; 赠送源代码:groovy-2.5.1-sources.jar;...人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。

    apache-groovy-sdk-2.4.11

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

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

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

    Groovy API docs 2.4.15 (CHM格式)

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

    Groovy入门经典 中英文版本

    Groovy入门经典 中英文版本,包含以下文件: Groovy Program.pdf Groovy入门经典.pdf 图书链接:http://product.china-pub.com/36984

    Groovy v2.4.13官方版

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

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

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

    Groovy入门经典.pdf

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

    apache-groovy-sdk-3.0.9.zip

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

    Groovy学习资料-中文.rar

    Groovy学习资料-中文.rar。Groovy学习资料。Grails学习资料。mht网页格式。

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

    Written by core members of the Groovy language team, this book presents Groovy like no other can—from the inside out. With relevant examples, careful explanations of Groovy's key concepts and ...

    Java中使用Groovy的三种方式

    Java中使用Groovy的三种方式,详细见我的博客。

    eclipse中的groovy插件

    eclipse中的groovy插件 eclipse中的groovy插件 实用

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

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

    microservices-spring-boot-groovy:使用 Spring Boot 和 Groovy 构建微服务

    微服务-spring-boot-groovy 使用 Spring Boot 和 Groovy 构建微服务创建这些项目是为了在当地的达拉斯 Groovy Grails 用户组会议上展示微服务架构这些服务使用您需要安装才能开始使用的各种外部服务。 您将需要安装 ...

    Groovy入门经典

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

Global site tag (gtag.js) - Google Analytics