簡體   English   中英

是否有可能在groovy中獲取閉包的調用者對象?

[英]Is it possible to get the caller object of a closure in groovy?

是否有可能在Closure的執行上下文中獲取調用ClosureObject引用?

例如:

public class Example {

    public Example(){
        def a = {return this};
        def b = [];

        b.metaClass.a = a;

        println b.a();
    }
}

我希望此執行返回b而不是Example

調用閉包的對象可以作為delegate引用。 例:

def a = { return delegate }
def b = []

b.metaClass.a = a

assert b.a() == b

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM