在浏览 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();
}
如您所见,方法定义中有 public abstract
,这是多余的,据我所知,不应包含在方法声明中。
我希望 JDK 团队看到这一点,并希望他们还在这里是有原因的。
因此问题来了,从接口(interface)声明中删除 public abstract
会破坏字节码兼容性吗?请记住,即使使用 JDK1.0 java 代码编写的代码,这个 Runnable
在技术上仍然必须工作。
最佳答案
我找不到更好的来源,但是 here's the JLS for Java 1. (也许 here 。)
与此后的所有其他 JLS 一样,它指出。
AbstractMethodDeclaration:
AbstractMethodModifiers opt ResultType MethodDeclarator Throwsopt ;
AbstractMethodModifiers:
AbstractMethodModifier
AbstractMethodModifiers AbstractMethodModifier
AbstractMethodModifier: one of
public abstract
注意 opt
。它还指出
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.
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.
它不会损害字节码兼容性。
他们可能只是想尽可能明确。
关于java - 从接口(interface)中省略公共(public)抽象会损害字节码兼容性吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23961756/