繁体   English   中英

groovy.lang.MissingPropertyException:无此类属性:类的所有者:母亲

[英]groovy.lang.MissingPropertyException: No such property: owner for class: Mother

我正在处理以下代码:

在“关闭出生方法”中的“ this.owner”处捕获到异常。

class Mother {
            int field = 1
            int foo(){
                return 2
            }
            Closure birth(param){
                def local = 3
                def closure = {caller -> [this,field,foo(),local,param,caller,this.owner]} 
                return closure
            }
        }

        Mother m = new Mother();
        closure = m.birth(4);

        context = closure.call(this);
        println context[0].class.name

        assert context[1..4] == [1,2,3,4]
        assert context[5] instanceof Script
        assert context[6] instanceof Mother

        fClosure = m.birth(4);
        sClosure = m.birth(4);
        assert false == fClosure.is(sClosure)

当运行完成时:

'this.owner'捕获到groovy.lang.MissingPropertyException:无此类属性:类的所有者:Mother

这是为什么?

一切正常,请在此处查看 ,特别是:

this对应于定义闭包的封闭类

由于相应的类是Mother ,并且没有owner字段或属性,因此MPE失败。 如果您在Mother类上定义owner ,它将运行(并可能因另一个异常而失败)。

暂无
暂无

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

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