我有一个问题,同时重写Groovy中的方法,同时更改返回类型(协变)。Groovy - Stackoverflow同时调用super.method()
我的测试代码:
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")
在上面的脚本,代码stackoverflows,只是反复调用子类中的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
我正在使用Groovy 2.1.5 - 它似乎在Groovy 2.2中工作。
有没有人知道是否有一个解决方法,使这项工作在Groovy 2.1.5或有一个错误号的详细信息,这已被修复作为其中的一部分?
它也适用于Groovy 2.3.3。 – Opal
我前段时间被那个人咬了,无法升级groovy。幸运的是,我可以改变子类的方法名称。 – Will