0

如果我动态地将一个属性添加到一个类中,则该类的每个实例都会使用对相同值的引用进行初始化(即使属性正确位于不同的地址,我也不希望它们共享相同的引用值):

这是一个例子:

class SolarSystem {

    Planets planets = new Planets()

    static main(args) {
        SolarSystem.metaClass.dynamicPlanets = new Planets()

        // Infinite loop
//        SolarSystem.metaClass.getDynamicPlanets = {
//            if (!delegate.dynamicPlanets.initialized) {
//                delegate.dynamicPlanets = new Planets(initialized: true)
//            }
//
//            delegate.dynamicPlanets
//        }

        // No such field: dynamicPlanets for class: my.SolarSystem
//        SolarSystem.metaClass.getDynamicPlanets = {
//            if (!delegate.@dynamicPlanets.initialized) {
//                delegate.@dynamicPlanets = new Planets(initialized: true)
//            }
//
//            delegate.@dynamicPlanets
//        }

        SolarSystem.metaClass.getUniqueDynamicPlanets = {
            if (!delegate.dynamicPlanets.initialized) {
                delegate.dynamicPlanets = new Planets(initialized: true)
            }

            delegate.dynamicPlanets
        }

//        SolarSystem.metaClass.getDynamicPlanets = {
//            throw new RuntimeException("direct access not allowed")
//        }

        def solarSystem1 = new SolarSystem()
        println "a ${solarSystem1.planets}"
        println "b ${solarSystem1.dynamicPlanets}"
        println "c ${solarSystem1.uniqueDynamicPlanets}"
        println "d ${solarSystem1.dynamicPlanets}"

        println ''

        def solarSystem2= new SolarSystem()
        println "a ${solarSystem2.planets}"
        println "b ${solarSystem2.dynamicPlanets}"
        println "c ${solarSystem2.uniqueDynamicPlanets}"
        println "d ${solarSystem2.dynamicPlanets}"
    }
}

在一个单独的文件中:

class Planets {
    boolean initialized = false
}

运行时,您会看到如下内容:

a my.Planets@4979935d
b my.Planets@66100363
c my.Planets@5e0feb48
d my.Planets@5e0feb48

a my.Planets@671ff436
b my.Planets@66100363
c my.Planets@651dba45
d my.Planets@651dba45

请注意,对于 solarSystem2,“普通”成员变量planets在创建两个对象时具有不同的地址。但是,动态添加的dynamicPlanets指向 solarSystem1 指向的同一对象(在本例中,地址为 66100363)。

我可以在我的动态 getter ( getUniqueDynamicPlanets ) 中重新分配它们,这样就解决了问题。

但是,我无法覆盖getDynamicPlanets getter,因为我要么获得无限循环,要么无法直接访问动态添加的属性。

有没有办法直接访问动态添加的属性,以便我可以在getDynamicPlanets getter 中处理这个?有没有更好的策略呢?抱歉,如果我错过了,我已经看了一堆...

谢谢

4

1 回答 1

0

I'm not 100% sure I understand your question, but if I do, did you try setting the getDynamicPlanets closure to have explicitly 0 parameters, so:

SolarSystem.metaClass.getDynamicPlanets = {->  ... }

If you don't have the -> with no args before it, there is an implicit it parameter that's assigned and it's not a zero arg method, so doesn't adhere to the javabean getter/setter pattern.

于 2012-07-12T00:02:05.083 回答