我有一個問題,同時重寫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