2015-09-01 33 views
1

匹配的构造函数,我定义的内部类功能构造,但我得到Could not find matching constructor for: C$Feature(java.lang.String),这里是我的代码:找不到在综合类

class C { 
    class Feature { 
     Feature(String ext) { 
      this.ext = ext 
     } 
     String ext 
    } 
} 

class C2 extends C { 
    def m() { 
     new Feature("smth") 
    } 
} 

class RoTry { 
    static void main(String[] args) { 
     new C2().m() 
    } 
} 

更新

我的常规版本

------------------------------------------------------------ 
Gradle 2.3 
------------------------------------------------------------ 

Build time: 2015-02-16 05:09:33 UTC 
Build number: none 
Revision:  586be72bf6e3df1ee7676d1f2a3afd9157341274 

Groovy:  2.3.9 
Ant:   Apache Ant(TM) version 1.9.3 compiled on December 23 2013 
JVM:   1.8.0_05 (Oracle Corporation 25.5-b02) 
OS:   Linux 3.13.0-24-generic amd64 
+0

你可能无法做出特色未做A.实例尝试把它放在你的'm'方法中:'new C.Feature(new C(),“smth”)' – Zarwan

+1

我试过你的代码,如果你删除了它编译的语法错误...... – Ilario

+1

应该是'new C.Feature(“smth”)' – cfrick

回答

1

非私有内部类需要构造函数中的形参:请参阅Do default constructors for private inner classes have a formal parameter?

因此,内部方法m()你应该使用new Feature(this, 'smth')

class C { 
    class Feature { 
     String ext 

     Feature(String ext) { 
      this.ext = ext 
     } 

     String toString() { 
      ext 
     }   
    } 

    def n() { 
     new Feature('nnnn') 
    } 
} 

class C2 extends C { 
    def m() { 
     new Feature(this, 'mmmm') 
    } 
} 

def c = new C() 
println c.n() 

def c2 = new C2() 
println c2.m() 

与反思,你可以看到它:

C.Feature.class.getDeclaredConstructors().each { constructor -> 
    println constructor 
} 

-- 

public C$Feature(C,java.lang.String) 
+0

这是我发现最好的方式 – nwaicaethi