Groovy-调用super.method()时堆栈溢出
我在更改返回类型(协变)时重写Groovy中的方法时遇到了一个问题 我的测试代码是:Groovy-调用super.method()时堆栈溢出,groovy,Groovy,我在更改返回类型(协变)时重写Groovy中的方法时遇到了一个问题 我的测试代码是: class Grandparent { public Grandparent doStuff( String s ){ println "GP $s" this } } class Parent extends Grandparent{ public Parent doStuff( String s ){ println "P $s "
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")
在上面的脚本中,代码stackoverflow,只是在子类上重复调用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) 我们在继承权中只有两个阶级(例如,切割儿童)
2) doStuff的所有实例都返回相同的类型,例如,所有返回GradParent
我使用的是Groovy2.1.5——它似乎在Groovy2.2中工作
有人知道Groovy 2.1.5中是否有解决方法,或者是否有作为其中一部分修复的bug编号的详细信息
有人知道在Groovy中是否有一种解决方法来实现这一点吗
2.1.5或是否有作为其中一部分修复的错误编号的详细信息
请参阅和。它在groovy 2.3.3中也可以正常工作。我不久前被它咬了一口,无法升级groovy。幸运的是,我可以更改子类方法的名称。谢谢-这个特殊的代码示例实际上在2.2中有效(第一个错误与静态方法有关,第二个错误看起来非常相似,但在2.2中被报告为已损坏)。尽管如此,答案还是有用的。