在Groovy中显式调用默认方法

时间:2019-02-22 08:23:59

标签: inheritance groovy java-8 interface default-method

这个问题与讨论Explicitly calling a default method in Java的另一个问题非常相似,但是使用Groovy代码(版本2.4.16)

public interface Interface { // a Java interface
    default void call() {}
}

public class Test implements Interface { // a Groovy class
    @Override
    public void call() {
        Interface.super.call() // compile error: Groovy:The usage of 'Class.this' and 'Class.super' is only allowed in nested/inner classes.
    }
}

另一方面,默认方法非常类似于Groovy中的Trait,下面的代码可以编译

public trait Trait { // a Groovy trait
    void call() {}
}

public class Test implements Trait { // a Groovy class
    @Override
    public void call() {
        Trait.super.call() // compiles OK
    }
}

那么,如何在Groovy子类中显式调用默认方法?

0 个答案:

没有答案