【发布时间】:2014-07-09 12:10:56
【问题描述】:
我在更改返回类型(协变)的同时覆盖 Groovy 中的方法时遇到了问题。
我的测试代码是:
class Grandparent {
public Grandparent doStuff( String s ){
println "GP $s"
this
}
}
class Parent extends Grandparent{
public Parent doStuff( String s ){
println "P $s "
this
}
}
class Child extends Parent{
public Child doStuff( String s ){
println "C $s "
super.doStuff(s)
this
}
}
Child c = new Child()
c.doStuff("Yo")
在上面的脚本中,代码堆栈溢出,只是重复调用 Child 类的 doStuff() 方法:
Caught: java.lang.StackOverflowError
java.lang.StackOverflowError
at Child.doStuff(GroovyInheritanceTest.groovy:18)
at Child.doStuff(GroovyInheritanceTest.groovy)
at Parent.doStuff(GroovyInheritanceTest.groovy)
at Child.super$3$doStuff(GroovyInheritanceTest.groovy)
at Child.doStuff(GroovyInheritanceTest.groovy:18)
at Child.doStuff(GroovyInheritanceTest.groovy)
at Parent.doStuff(GroovyInheritanceTest.groovy)
如果
,则不会发生此问题1)我们在层次结构中只有两个类(例如,切出 Child) 2) doStuff 的所有实例都返回相同的类型,例如全部返回 GradParent
我使用的是 Groovy 2.1.5 - 它似乎在 Groovy 2.2 中工作。
有谁知道是否有解决方法可以在 Groovy 2.1.5 中进行这项工作,或者有作为其中一部分修复的错误编号的详细信息?
【问题讨论】:
-
它也适用于 groovy 2.3.3。
-
前段时间我被那个咬了,无法升级groovy。幸运的是,我可以更改子类方法名称。
标签: groovy