闭包(Closure)
def closure = {
println 'groovy'
}
println closure.getClass() //Test$_run_closure1
closure.call() //groovy
closure() //groovy
//默认一个参数
def closure = {
println it
}
closure('Groovy') //Groovy
def closure = {
String i,String j-> //不写参数类型也可以
println i + j
}
closure("hello","world")
//输出:helloworld
def closure = {
String i = "hello",String j->
println i + j
}
closure("hello","world") //helloworld
closure("world") //helloworld
//柯里化闭包(就是个参数绑定一个值)
def closure = {
String i = "hello",String j->
println i + j
}
def closure2 = closure.ncurry(0,"Groovy") //第0个参数是Groovy
closure2("Java") //GroovyJava
def func(closure) {
closure()
}
interface Action {
void call()
}
func(new Action() {
@Override
void call() {
println 'call'
}
})
//输出 call
class Action1{
def call() {
println 'call'
}
}
new Action1()()
//输出 call
调用一个对象的"()"方法,就是调用了该对象上的call()方法
def closure = {
int i,String j->
}
println closure.parameterTypes //[int, class java.lang.String]
println closure.maximumNumberOfParameters //2
this/owner/delegate
def closure = {
println this
println owner
println delegate
}
closure()
//输出
//Test@e50a6f6
//Test@e50a6f6
//Test@e50a6f6
输出的是Test的一个对象,分析:查看编译后的Test.class文件
public class Test extends Script {
...
public Object run() {
//内部类
class NamelessClass_1 extends Closure implements GeneratedClosure {
· public NamelessClass_1(Object _outerInstance, Object _thisObject) {
...
super(_outerInstance, _thisObject);
}
}
//创建closure的地方
Object closure = new NamelessClass_1(this, this);
...
}
...
}
创建Closure的时候传递了两个参数,都是this,this就是当前的类Test,下面是创建closeure是调用的super方法
public Closure(Object owner, Object thisObject) {
this.owner = owner;
this.delegate = owner;
this.thisObject = thisObject;//这个就是闭包中的this
...
}
owner,delegate,this都是Test这个类
class TestClosure {
def static closure = {
println "this : " + this
println "owner : " + owner
println "delegate : " + delegate
}
}
TestClosure.closure()
//输出
//this : class TestClosure
//owner : class TestClosure
//delegate : class TestClosure
输出的是Class的对象,不是实例对象
class TestClosure {
def closure1 = {
def closure2 = {
println "this : " + this
println "owner : " + owner
println "delegate : " + delegate
}
closure2()
}
}
new TestClosure().closure1()
//输出
//this : TestClosure@2b6faea6
//owner : TestClosure$_closure1@59d016c9
//delegate : TestClosure$_closure1@59d016c9
分析编译后的文件:
public class TestClosure implements GroovyObject {
public TestClosure() {
...
TestClosure._closure1 var2 = new TestClosure._closure1(this, this);//创建closure1
...
}
class _closure1 extends Closure implements GeneratedClosure {
public _closure1(Object _outerInstance, Object _thisObject) {
...
super(_outerInstance, _thisObject);
}
public Object doCall(Object it) {
...
class NamelessClass_1 extends Closure implements GeneratedClosure {
...
super(_outerInstance, _thisObject);
}
//this就是closure1对象,this.getThisObject()就是closure1中的this,也就是TestClosure对象
Object closure2 = new NamelessClass_1(this, this.getThisObject());
...
}
}
}
public Closure(Object owner, Object thisObject) {
this.owner = owner;
this.delegate = owner;
this.thisObject = thisObject;//这个就是闭包中的this
...
}
TestClosure的构造方法中创建了closure1对象,在closure1中的doCall方法中创建了closure2对象,创建closure2对象的构造函数传入了this和this.getThisObject(),this.getThisObject()就是closure1中的this,也就是TestClosure对象。
this:定义它的时候 所在的类的this,静态闭包当中为class
owner:定义它的时候 所在类的对象
delegate:默认就是owner
delegate的作用
class TestFunc{
def func() {
print 'func'
}
}
def closure = {
func()
}
closure.delegate = new TestFunc()
closure()
//输出: func
修改closure的代理,在代理中找func()方法
代理策略:
class TestFunc{
def func() {
print 'func1'
}
}
def func() {
print 'func2'
}
def closure = {
func()
}
closure.delegate = new TestFunc()
closure.resolveStrategy = Closure.DELEGATE_FIRST //去掉这行输出func2
closure()
//输出: func1
resolveStrategy:优先策略,默认为OWNER_FIRST。
网友评论