Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
groovy singleton访问重载构造函数_Groovy - Fatal编程技术网

groovy singleton访问重载构造函数

groovy singleton访问重载构造函数,groovy,Groovy,groovy Singleton(注意,在groovy 2.6中,如果要包含显式构造函数,必须将strict设置为false) test.groovy def test1 = test.instance 当我发布以下声明时,它对我有效,我可以看到它的解 构造函数被调用 如何在使用第二个construcor参数时创建实例 如果我发布 def test2 = test("anish").instance 这让我犯了一个错误,我该如何解决这个问题呢 groovy.lang.MissingMeth

groovy Singleton(注意,在groovy 2.6中,如果要包含显式构造函数,必须将strict设置为false)

test.groovy

def test1 = test.instance
当我发布以下声明时,它对我有效,我可以看到它的解 构造函数被调用

如何在使用第二个construcor参数时创建实例

如果我发布

def test2 = test("anish").instance 
这让我犯了一个错误,我该如何解决这个问题呢

groovy.lang.MissingMethodException: No signature of method: test.test() is applicable for argument types: (java.lang.String) values: [anish]
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:54)
    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:78)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:44)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:143)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:151)

在第一种情况下,您访问的是静态属性
test.instance
,它依次调用静态方法
test.getInstance()
。在第二种情况下,看起来您正试图将第二个构造函数作为方法调用。这是无效的groovy:您需要使用
new
关键字来创建一个实例,从而触发构造函数。此外,将构造函数设为私有会使其无法访问,除非在类本身内部


如果需要实例化另一个实例,它可能首先不应该是一个单实例。

在第一种情况下,您访问的是静态属性
test.instance
,该属性反过来调用静态方法
test.getInstance()
。在第二种情况下,看起来您正试图将第二个构造函数作为方法调用。这是无效的groovy:您需要使用
new
关键字来创建一个实例,从而触发构造函数。此外,将构造函数设为私有会使其无法访问,除非在类本身内部


如果您需要实例化另一个实例,它首先可能不应该是单例。

请包含错误。我必须看一下实际的转换,但我不确定它是否适用于非默认的构造函数。哥们!如果您不了解模式的用途,为什么要使用它?声明一个类singleton的全部目的是限制它只有一个instancePlease包含错误。我必须看一下实际的转换,但我不确定它是否适用于非默认的构造函数。哥们!如果您不了解模式的用途,为什么要使用它?声明一个类singleton的全部目的是限制它只有一个实例+1 esp用于最后一句话+1 esp用于最后一句话
groovy.lang.MissingMethodException: No signature of method: test.test() is applicable for argument types: (java.lang.String) values: [anish]
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:54)
    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:78)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:44)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:143)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:151)