可以从接口中省略公共摘要会损害字节码兼容性吗?

ski*_*iwi 4 java compatibility interface backwards-compatibility

在浏览SO问题时,我的定义是Runnable:

@FunctionalInterface
public interface Runnable {
    /**
     * When an object implementing interface <code>Runnable</code> is used
     * to create a thread, starting the thread causes the object's
     * <code>run</code> method to be called in that separately executing
     * thread.
     * <p>
     * The general contract of the method <code>run</code> is that it may
     * take any action whatsoever.
     *
     * @see     java.lang.Thread#run()
     */
    public abstract void run();
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,它public abstract在方法定义中具有多余的功能,据我所知,不应包含在方法声明中.

我希望JDK团队已经看到了这一点,并期望它们仍然存在的原因.

因此问题,可以public abstract从接口声明中删除打破字节码兼容性吗?请记住,Runnable即使使用JDK1.0 java代码编写的代码,这在技术上仍然必须工作.

Sot*_*lis 5

我找不到更好的来源,但这里是JLS for Java 1.(可能在这里.)

与其他所有JLS一样,它表示.

AbstractMethodDeclaration:

    AbstractMethodModifiers opt ResultType MethodDeclarator Throwsopt ;

AbstractMethodModifiers:

    AbstractMethodModifier

    AbstractMethodModifiers AbstractMethodModifier

AbstractMethodModifier: one of

    public abstract
Run Code Online (Sandbox Code Playgroud)

请注意opt.它还说明了

接口主体中的每个方法声明都是隐式抽象的,因此它的主体始终用分号表示,而不是块.为了与旧版本的Java兼容,允许但不鼓励在样式上冗余地为接口中声明的方法指定抽象修饰符.

接口主体中的每个方法声明都是隐式公开的.作为一种风格问题,允许冗余地指定接口方法的公共修饰符.

它不会损害字节码兼容性.

他们可能只想尽可能明确.