Fed*_*icz 4 groovy constructor abstract-class subclass
我有一个抽象的java类,它有一个构造函数,我将它从一个groovy类扩展.(我的想法是将java类保留为应用程序内部的契约,并加载实现某些构造函数和方法的外部groovy类)
我如何强制Groovy实现抽象超类的构造函数?Groovy是否允许强制实现抽象父类的构造函数?
问题是Eclipse Groovy IDE并没有强迫我在子类中实现父类的构造函数,我希望Groovy会自动创建它,因此这就是不强制它的原因.但是,在运行时尝试使用java反射获取构造函数失败时,如果我没有在子类中定义父构造函数,则会失败.
(我在Groovy有0经验)
它看起来像编译器中未经检查的情况.在反编译时,扩展类获得一个空构造函数.测试应该让你了解,因为这种情况在运行时不起作用.
我不知道如何使用这个类; 我尝试了我所知道的方式:
abstract class AbstractClass {
String string
Integer integer
AbstractClass(String string, Integer integer) {
this.string = string
this.integer = integer
}
}
class ImplClass extends AbstractClass { }
// every constructor fails
abs1 = new ImplClass('a', 1)
abs2 = [string: 'b', integer: 2] as ImplClass
abs3 = new ImplClass(abs: 'c', a: 3)
abs4 = ImplClass [string:'d', integer:4]
Run Code Online (Sandbox Code Playgroud)
它们都没有在运行时工作,但编译得很好;-).情况更多的是编译错误与运行时错误.也许填写JIRA?
相反,如果你需要继承构造函数,你可以@groovy.transfom.InheritConstructors在扩展类中使用.这样您就可以使用构造函数而无需super()显式调用.