我正在閱讀有關Java 8中的默認方法,而且我陷入了一件事 - 有沒有一種方法可以從界面調用默認方法而不實現它,或者使用動態代理?只用一個簡單的方法,就像下面的方法:如何顯式調用默認方法,沒有反射和動態代理?
interface DefaultTestInterface{
default void method1(){
//default method
}
}
class ImplementingClass implements DefaultTestInterface{
public void method1(){
//default method invocation in implementing method
DefaultTestInterface.super.method1();
}
void method2(){
//default method invocation in implementing class
DefaultTestInterface.super.method1();
}
}
public class Main {
public static void main(String[] args) {
//is there any way to simply invoke default method without using proxy and reflection?
}
}
我看過類似的問題,但first在實現方法只調用連接,另外兩個與dynamic Proxy using reflection和 reflection連接。
這些解決方案相當複雜,我想知道是否有更簡單的方法。我也閱讀了這些文章,但是我沒有找到解決我的問題的方法。我會很感激任何幫助。
所以你想使用一個沒有實例的實例方法??? – fabian
你的問題存在一些困惑。 java 8接口的*默認方法*已經是一個實現。如果您沒有需要不同實現的類特定行爲,則只需實現該接口,然後省略要用作默認的方法。如果您想從* interface *中調用方法,而不是考慮使用* static *關鍵字來定義它,請改爲。 –
沒有@fabian,我正在考慮以某種方式包含實例化的解決方案。 –