[英]why interfaces can be implemented in abstract classes but not in other interfaces?
[英]Can omitting public abstract from interfaces harm bytecode 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();
}
正如您所看到的,它在方法定义中有public abstract
,这是多余的, 据我所知 ,不应该包含在方法声明中。
我希望JDK团队已经看到了这一点,并期望它们仍然存在的原因。
因此,问题是,可以从接口声明中删除public abstract
破坏字节码的兼容性吗? 请记住,即使使用JDK1.0 java代码编写的代码,此Runnable
技术上仍然必须工作。
我找不到更好的来源,但这里是JLS for Java 1. (可能在这里 。)
与其他所有JLS一样,它表示。
AbstractMethodDeclaration:
AbstractMethodModifiers opt ResultType MethodDeclarator Throwsopt ;
AbstractMethodModifiers:
AbstractMethodModifier
AbstractMethodModifiers AbstractMethodModifier
AbstractMethodModifier: one of
public abstract
注意opt
。 它还说明了
接口主体中的每个方法声明都是隐式抽象的,因此它的主体始终用分号表示,而不是块。 为了与旧版本的Java兼容,允许但不鼓励在样式上冗余地为接口中声明的方法指定抽象修饰符。
接口主体中的每个方法声明都是隐式公开的。 作为一种风格问题,允许冗余地指定接口方法的公共修饰符。
它不会损害字节码兼容性。
他们可能只想尽可能明确。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.