Groovy instance.metaclass vs this.metaclass [英] Groovy instance.metaclass vs this.metaclass
问题描述
我有脚本:
任务myTask {}
类Person {
Person(){
Person instance = this
println this.metaClass.class.name
println this.getMetaClass()。class.name
println instance .metaClass.class.name
println instance.getMetaClass()。class.name
}
}
Person person = new Person()
结果如下:
groovy.lang.MetaClassImpl
groovy.lang.MetaClassImpl
org.codehaus.groovy.runtime.HandleMetaClass
org.codehaus.groovy.runtime.HandleMetaClass
任何人都可以向我解释发生了什么事情?
。
查看 class
,
class Person {
def a,b
Person(){
a =这个
b = t他的
printlnthis $ this
printlna $ a
printlnb $ b
}
def printAll(){
printlnthis.metaClass $ {this.metaClass}
printlnthis.class.metaClass $ {this.class.metaClass}
printlna.metaClass $ {a.metaClass}
printlnb.metaClass $ {b.metaClass}
}
查看 groovysh code>。它可能会给你一些关于发生了什么的暗示。
-
p
和q
是两个不同的对象,但是
-
p.metaClass
与q.metaClass
和 -
printAll
打印完全相同的东西,p
和q
-
a.metaClass
和b.metaClass
持有this.class.metaClass
,而不是this.metaClass
,您会看到
只有一个由 MetaClassImpl
,也是 HandleMetaClass
中的一个,用于 Person
。无论您实例化 Person
多少次,它们都将被分配给实例。但是,当你展开任何一个实例时,只有一个新的 HandleMetaClass
对象才会被创建 - 仅仅针对那个特定的对象;这次 HandleMetaClass
将不包含 MetaClassImpl
,但 ExpandoMetaClass
代替。
请参阅下面的截图,
现在,回答你的问题, this.metaClass
是一个特例,就像这
本身。它不会为您提供 HandleMetaClass
对象的句柄,因此您无法展开 metaClass
of 这个
- 直接;而且也没有任何意义,因为那么所有其他未来的例子都会分享这种扩张。
如果你确实需要这种行为,那么你可以将这个
传递给其他变量,即 instance = this
,就像你在构造函数中所做的那样,然后你可以扩展实例
- 并且这个扩展对于这个
以及所有其他未来的实例。但是,那么为什么不把这种行为添加到课堂本身呢?为什么要扩展?
i have the flowing script:
task myTask {}
class Person {
Person() {
Person instance = this
println this.metaClass.class.name
println this.getMetaClass().class.name
println instance.metaClass.class.name
println instance.getMetaClass().class.name
}
}
Person person = new Person()
And the output is :
groovy.lang.MetaClassImpl
groovy.lang.MetaClassImpl
org.codehaus.groovy.runtime.HandleMetaClass
org.codehaus.groovy.runtime.HandleMetaClass
can anyone explain to me what is going on ?
thanks in advance.
Take a look at this class
,
class Person {
def a, b
Person() {
a = this
b = this
println "this $this"
println "a $a"
println "b $b"
}
def printAll() {
println "this.metaClass ${this.metaClass}"
println "this.class.metaClass ${this.class.metaClass}"
println "a.metaClass ${a.metaClass}"
println "b.metaClass ${b.metaClass}"
}
}
Take a look at the screenshot of the groovysh
. It might give you a little hint about what's going on.
p
andq
are two different objects, butp.metaClass
is the same asq.metaClass
, andprintAll
prints exactly the same thing for both,p
andq
a.metaClass
andb.metaClass
are holdingthis.class.metaClass
, notthis.metaClass
, you see
There is only one object created of MetaClassImpl
, and also only one of HandleMetaClass
, for Person
. And no matter how many times you instantiate Person
, they will be assigned to the instance. But, when you expand any of that instance, only then a new HandleMetaClass
object will be created -- just for that particular object; and this time HandleMetaClass
will be holding not MetaClassImpl
, but ExpandoMetaClass
instead.
See the screenshot below,
Now, to answer your question, this.metaClass
is a special case, like this
itself. It doesn't give you the handle, HandleMetaClass
object, so you can't expand over metaClass
of this
-- directly; and there is no sense in that either, because then all other future instances will share that expansion.
If you really want that behaviour then you can pass this
to some other variable, i.e. instance = this
, as you did in the constructor, and then you can expand over that instance
-- and that expansion would be true for this
and all other future instances. But then, why not add the behaviour to class itself, in the first place. Why expand?
这篇关于Groovy instance.metaclass vs this.metaclass的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!