简体   繁体   English

如何获取在Groovy中调用闭包的类(范围)?

[英]How do I get the class (scope) from which a closure is called in Groovy?

class Box {
    Closure click

    Box () {
        click = {}
    }

    void onClick() {
        click()
    }
}

class TextBox extends Box {
    List<String> content

    TextBox (String[] a) {
        super()
        content = a
    }
}

class Main {
    public static void main(String[] args) {
        Main m = new Main()
    }

    Main() {
        String[] a = ["Hello world!"]
        Box b = new TextBox(a)
        b.click = {content.add("You clicked this box!")}
        b.onClick()  //throws Exception
    }
}

(The above is, obviously, a simplification; in reality, the classes are a bit more involved, and calling of onClick() is due to a click on a JFrame) (显然,上面是一个简化;实际上,类更多涉及,并且onClick()的调用是由于点击JFrame)

Now, when I try to run that (ie run Main.main()), I get an exception: Exception in thread "AWT-EventQueue-0" groovy.lang.MissingPropertyException: No such property: content for class: Main 现在,当我尝试运行它(即运行Main.main())时,我得到一个异常:线程“AWT-EventQueue-0”中的异常groovy.lang.MissingPropertyException:没有这样的属性:类的内容:Main

Clearly, it is, for some reason, searching for the List in Main, not in TextBox or Box, from where it is called. 显然,出于某种原因,它是在Main中搜索List,而不是在TextBox或Box中搜索它的位置。 I also tried using this, owner and delegate, but they all point to Main as well. 我也试过使用这个,所有者和代表,但他们都指向Main。 I managed to have it work by giving this as an argument: 我设法让它作为一个参数:

...
void onClick() {
    click(this)
}
...
b.click = {it.content.add("You clicked this box!")}

It seems, however, weird to actually need to pass "this" to a closure just for it to be able to know where it was called from. 然而,实际上需要将“this”传递给闭包只是因为它能够知道从哪里调用它,这似乎很奇怪。 Isn't there a more elegant solution? 难道没有更优雅的解决方案吗? Also, even if it is indeed impossible to get into the TextBox-scope, is it somehow possible to get into the Box-scope? 而且,即使确实无法进入TextBox范围,是否有可能进入Box-scope?

See closures groovy docs. 请参阅闭包 groovy文档。 Pay attention on Implicit variables : this, owner, and delegate. 注意隐式变量 :this,owner和delegate。
Edit : Fix delegate before call: 编辑 :在调用之前修复委托:

b.click = {content.add("You clicked this box!")}
b.click.delegate = b
Main() {
    String[] a = ["Hello world!"]
    Box b = new TextBox(a)
    println "1:- $b.click.delegate" //Will print TextBox
    b.click = {
        println "2:- $b.click.delegate" //Will print Main

        //Since the closure is now defined inside Main(), 
        //Main becomes the delegate. Reset the delegate to TextBox.
        b.click.delegate = b

        println "3:- $b.click.delegate" //Will print TextBox
        content.add("You clicked this box!")
    }
    println "4:- $b.click.delegate" //Will print Main
    b.onClick()

    println b.content //Will print [Hello world!, You clicked this box!]
}

//Output:
1:- TextBox@c166770
4:- Main@6dbdc863
2:- Main@6dbdc863
3:- TextBox@c166770
[Hello world!, You clicked this box!]

Points to note: 注意事项:

  • Sequence 4 is present before 2 and 3 since the closure is not called/invoked at that point of time. 序列4出现在2和3之前,因为在那个时间点没有调用/调用闭包。
  • Sequence 1 prints TextBox but Sequence 4 prints Main . 序列1打印TextBox但序列4打印Main Note that the delegate has changed (after defining closure b.click ) from TextBox to Main by now. 请注意, delegate已经从TextBox更改(在定义闭包b.click )到Main

To make things easier, instead of changing things in Main , you can just modify onClick() as 为了onClick() ,您可以将onClick()修改为,而不是在Main中更改内容

void onClick() {
    click.delegate = this
    click()
}

Refer this script for details. 有关详细信息,请参阅此脚

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM