从 Groovy 中的子类调用父特征方法



>我有以下内容:

trait Trait {
def Method() {
// do stuff
}
}
class Class implements Trait {
def Method() {
// do other stuff
super.Method()
}
}

这会编译但不运行,因为 Groovy 无法解析超级。方法((。仅调用 Method(( 会导致堆栈溢出。

是否可以以这种方式覆盖Groovy方法?

你可以这样做(使用提到的命名约定,但这不是问题(

trait Trait {
def method() {
println "yay"
}
}
class MyClass implements Trait {
def method() {
// do other stuff
println "woo"
Trait.super.method()
}
}
new MyClass().method() 

最新更新