如何在构造函数中初始化默认值:groovy

时间:2018-04-08 16:24:25

标签: groovy

代码:

@groovy.transform.InheritConstructors
    private class PSLConfig {
        //////////////////////////// Configuration ////////////////////////////
        ConfigManager cm = ConfigManager.getManager()
        public ConfigBundle cb = cm.getBundle("Baseline")

        public Boolean createNewDatastore;
        ////////////////////////  Model description ///////////////////////////
        public double initialWeight = 1;
        public boolean sq = true;

        public def preds;
        public def pred_to_filepred;

        public PSLConfig(ConfigBundle cb) {
            this.cb = cb;

            this.createNewDatastore = true;

            this.preds = [Block, Active, Device, GeoLoc, IP, Browser, URL1, SimURL, SimIP, VeryClose, \
                    DevActive, DevBrw, UserSpaceTime, Far];
            this.pred_to_filepred = [Block:'BLOCK', Active:'ACTIVE', Device:'DV', GeoLoc:'LOC', IP:'IP', \
                                Browser:'BR', URL1:'URL', SimURL:'URLSIM', SimIP:'IPSIM', VeryClose:'VeryClose', \
                                DevActive:'DEVACTIVE', DevBrw:'DEVBRW', UserSpaceTime:'SPACETIME', Far:'Far'];
        }
    }

我收到错误:

Exception in thread "main" groovy.lang.MissingPropertyException: No such property: Block for class: org.tucci.psl.coll.hybrid.HybridCCM1
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:51)
    at org.codehaus.groovy.runtime.callsite.GetEffectivePogoPropertySite.getProperty(GetEffectivePogoPropertySite.java:84)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:304)
    at org.tucci.psl.coll.hybrid.HybridCCM1$PSLConfig.<init>(HybridCCM1.groovy:110)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:77)
    at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:102)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:57)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:232)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:252)
    at org.tucci.psl.coll.hybrid.HybridCCM1.<init>(HybridCCM1.groovy:640)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:77)
    at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:102)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:57)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:232)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:244)
    at org.tucci.psl.coll.hybrid.HybridCCM1.main(HybridCCM1.groovy:647)
[2018-04-08T20:53:33+0530]: Failed to run

错误在于构造函数中的值的初始化。错误at org.tucci.psl.coll.hybrid.HybridCCM1$PSLConfig.<init>(HybridCCM1.groovy:110)中提到的第110行是在构造函数中定义this.preds的值的行。我不明白如何正确初始化def值。请解释一下。

1 个答案:

答案 0 :(得分:0)

您的preds需要成为字符串。稍后在代码中,您将它们用作Map中的字符串键。 E.g。

[Block:'BLOCK', ...]

只是&#34;简短&#34;为:

['Block':'BLOCK', ...]