为什么这个元类只在构造函数执行后才修改值?

Why is this metaclass modifying the values only after the constructor executes?

class EmailAssist {
    def abcService = "abc"
    String name

    EmailAssist(String name) {
        this.name = name
        println abcService
    }

    EmailAssist() {
    }

}

EmailAssist.metaClass.getAbcService = {->
    "test"
}

def e = new EmailAssist("Joe")
println e.abcService

这导致

的输出
abc
test

我认为这会导致

test
test

谁能解释一下到底是怎么回事?元类是否仅在构造函数执行后修改,而不是覆盖构造函数方法?

如果您在定义字段的 class(不是 inner/nested class,不是开放块)中,并且您使用限定或非限定 this,则直接访问,不考虑MOP。因此,您不能以这种方式隐藏字段。由于此逻辑是由编译器和文字完成的,而不是按值,您可以通过 def tmp = this; tmp.abcService

绕过它