关于java:jdk9jdk17新特性学习私有接口方法

在 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();
    }
}

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理