1

在 Java 8,接口可以有常量变量和抽象方法。可以通过default关键字,在接口里写方法的代码,但没办法写私有方法。
在Java 9 ,支持在接口里写私有方法了。示例代码如下:

public interface MyInterface {
    private void  test(){
        System.out.println("private test method");
    }
    default void doTest(){
        test();
    }
    public static void main(String[] args){
        MyInterface myinterface = new MyInterface() {
            @Override
            public void doTest() {
                MyInterface.super.doTest();
            }
        };
        myinterface.doTest();
    }
}

竣峰
164 声望3 粉丝