groovyd的HandleMetaClass何时实例化,以及为什么在创建新对象时会改变

时间:2019-04-26 01:22:09

标签: groovy metaclass mop

我不明白为什么创建HandleMetaClass对象,并且当添加新的属性和方法时,新创建的对象HandleMetaClass变为另一个实例化对象。为什么this.metaclass与a.aclass不同?

import groovy.transform.Trait
import org.codehaus.groovy.runtime.HandleMetaClass
import org.codehaus.groovy.runtime.InvokerHelper


class Person {

    def test() {
        def a = this
        def b = a
        println "a.metaClass-------->>>" + a.metaClass
        println "b.metaClass-------->>>" + b.metaClass
        println "this.metaClass-------->>>" + this.metaClass
    }
}


def person = new Person()

//println person.saysd()

println "Person.metaClass-------->>>" + Person.metaClass

println "person.metaClass-------->>>" + person.metaClass
person.test()


person.metaClass.d="@"

println "person.metaClass-------->>>" + person.metaClass



def newperson = new Person()
println "newperson.metaClass-------->>>" + person.metaClass



输出如下:

Person.metaClass-------->>>org.codehaus.groovy.runtime.HandleMetaClass@21e360a[groovy.lang.MetaClassImpl@21e360a[class Person]]
person.metaClass-------->>>org.codehaus.groovy.runtime.HandleMetaClass@21e360a[groovy.lang.MetaClassImpl@21e360a[class Person]]
a.metaClass-------->>>org.codehaus.groovy.runtime.HandleMetaClass@21e360a[groovy.lang.MetaClassImpl@21e360a[class Person]]
b.metaClass-------->>>org.codehaus.groovy.runtime.HandleMetaClass@21e360a[groovy.lang.MetaClassImpl@21e360a[class Person]]
this.metaClass-------->>>groovy.lang.MetaClassImpl@21e360a[class Person]
person.metaClass-------->>>org.codehaus.groovy.runtime.HandleMetaClass@1b11171f[groovy.lang.ExpandoMetaClass@1b11171f[class Person]]
newperson.metaClass-------->>>org.codehaus.groovy.runtime.HandleMetaClass@1b11171f[groovy.lang.ExpandoMetaClass@1b11171f[class Person]]

0 个答案:

没有答案