【发布时间】:2019-06-30 08:55:17
【问题描述】:
我的问题是关于interface。我创建了一个接口,定义了四个方法:第一个方法是private方法;第二个是default 方法;第三个是static 方法;第四个是abstract 方法。
编译此接口并检查其配置文件后:default 方法转换为public 方法,static 和abstract 方法都有一个前置public 修饰符。这是为什么呢?
代码:
interface InterfaceProfile {
private void privateM() { //this method is hidden
System.out.println("private Method");
}
default void defaultM() {
System.out.println("Default Method");
}
static void staticM() {
System.out.println("Static Method");
}
void doStuff(); //by default adds the public modifier
}
InterfaceProfile 类
D:\Linux\IDE\Workspace\OCA-Wrokspace\Ocaexam\src>javap mods\com\doubt\session\InterfaceProfile.class
Compiled from "InterfaceProfile.java"
interface com.doubt.session.InterfaceProfile {
public void defaultM();
public static void staticM();
public abstract void doStuff();
}
【问题讨论】:
-
AFAIK,Java 9 引入了私有接口方法。
-
其实我用的是openjdk 12
标签: java interface java-9 method-modifier