简体   繁体   English

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

[英]Can omitting public abstract from interfaces harm bytecode compatibility?

While browsing through SO questions, I came accross the definition of Runnable : 浏览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();
}

As you can see, it has public abstract in the method definition, which are superfluous and, as far as I know , should not be included in method declarations. 正如您所看到的,它在方法定义中有public abstract ,这是多余的, 据我所知 ,不应该包含在方法声明中。

I expect the JDK team to have seen this and expect there to be a reason they are still here. 我希望JDK团队已经看到了这一点,并期望它们仍然存在的原因。

Hence the question, can removing public abstract from interface declarations break bytecode compatability? 因此,问题是,可以从接口声明中删除public abstract破坏字节码的兼容性吗? Keep in mind that this Runnable technically still has to work even with code written using JDK1.0 java code. 请记住,即使使用JDK1.0 java代码编写的代码,此Runnable技术上仍然必须工作。

I can't find any better source, but here's the JLS for Java 1. (Maybe here .) 我找不到更好的来源,但这里是JLS for Java 1. (可能在这里 。)

Like every other JLS since then, it states. 与其他所有JLS一样,它表示。

AbstractMethodDeclaration:

    AbstractMethodModifiers opt ResultType MethodDeclarator Throwsopt ;

AbstractMethodModifiers:

    AbstractMethodModifier

    AbstractMethodModifiers AbstractMethodModifier

AbstractMethodModifier: one of

    public abstract

Notice the opt . 注意opt It also states 它还说明了

Every method declaration in the body of an interface is implicitly abstract, so its body is always represented by a semicolon, not a block. 接口主体中的每个方法声明都是隐式抽象的,因此它的主体始终用分号表示,而不是块。 For compatibility with older versions of Java, it is permitted but discouraged, as a matter of style, to redundantly specify the abstract modifier for methods declared in interfaces. 为了与旧版本的Java兼容,允许但不鼓励在样式上冗余地为接口中声明的方法指定抽象修饰符。

Every method declaration in the body of an interface is implicitly public. 接口主体中的每个方法声明都是隐式公开的。 It is permitted, but strongly discouraged as a matter of style, to redundantly specify the public modifier for interface methods. 作为一种风格问题,允许冗余地指定接口方法的公共修饰符。

It will not harm bytecode compatibility. 它不会损害字节码兼容性。

They probably just wanted to be as explicit as possible. 他们可能只想尽可能明确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM