为什么要将 Java 接口方法声明为抽象方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/641536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why would one declare a Java interface method as abstract?
提问by Uri
I used the "pull interface" refactoring feature of Eclipse today to create an interface based on an existing class. The dialog box offered to create all the new methods of the new interface as "abstract" methods.
我今天使用了 Eclipse 的“拉接口”重构特性,基于现有类创建了一个接口。该对话框提供将新界面的所有新方法创建为“抽象”方法。
What would be the benefit of that?
这样做有什么好处?
I thought that the fact that you were allowed to declare interface methods as abstract was a superfluous and harmless feature of the language that is not particularly encouraged.
我认为允许将接口方法声明为抽象的这一事实是该语言的一个多余且无害的特性,但并不特别鼓励。
Why would Eclipse support such a style, or why would someone voluntarily choose to do so?
为什么 Eclipse 会支持这种风格,或者为什么有人会自愿选择这样做?
Clarification: I am not asking why interface methods are abstract, that is obvious. I am asking why one would explicitly choose to mark them as abstract since if they're in an interface they are abstract anyway.
澄清:我不是问为什么接口方法是抽象的,这是显而易见的。我在问为什么人们会明确选择将它们标记为抽象,因为如果它们在接口中,无论如何它们都是抽象的。
采纳答案by jdmichal
According to the Java Language Specification, the abstract
keyword for interfaces is obsolete and should no longer be used. (Section 9.1.1.1)
根据Java 语言规范,abstract
接口关键字已过时,不应再使用。(第 9.1.1.1 节)
That said, with Java's propensity for backwards compatibility, I really doubt it will ever make a difference whether the abstract
keyword is present.
也就是说,由于 Java 具有向后兼容性的倾向,我真的怀疑是否abstract
存在关键字会有所不同。
回答by Daniel Hiller
According to JLS methods in interfaces are abstract by default, so the keyword is redundant. Knowing this, I'd never use it to "avoid presentational clutter".
由于接口中的 JLS 方法默认是抽象的,所以关键字是多余的。知道这一点,我永远不会用它来“避免展示混乱”。
回答by VonC
"The benefice of that" (adding abstract on interface methods declaration) in eclipse would be an old compatibility issue with jdt eclipse compiler in jdk1.3
eclipse中的“好处”(在接口方法声明上添加抽象)将是jdk1.3中jdt eclipse编译器的旧兼容性问题
Since 1.4, jdk libraries are no longer containing default abstract methods (on abstract classes implementing interfaces).
This is fooling the Eclipse 1.3 compiler diagnosis since their implementation is relying on their existence.
Note that Javac 1.3 would refuse altogether to perform against 1.4 libraries (using -bootclasspath option).
从 1.4 开始,jdk 库不再包含默认抽象方法(在实现接口的抽象类上)。
这是愚弄 Eclipse 1.3 编译器诊断,因为它们的实现依赖于它们的存在。
请注意,Javac 1.3 将完全拒绝对 1.4 库执行(使用 -bootclasspath 选项)。
Since the Eclipse compiler is likely to be in 1.4 compliance level (see Workbench>Preferences>Java>Compiler>JDK Compliance
), or use at least 1.3 class libraries if using 1.3 compliance mode, the presence of "abstract" is not required in most of the current eclipse projects.
由于 Eclipse 编译器很可能处于 1.4 遵从级别(请参阅 参考资料Workbench>Preferences>Java>Compiler>JDK Compliance
),或者如果使用 1.3 遵从模式则至少使用 1.3 类库,因此在当前的大多数 eclipse 项目中不需要“抽象”的存在。
回答by Will
From the Java SE 7 JLS(Java Language Specification): "It is permitted, but discouraged as a matter of style, to redundantly specify the public and/or abstract modifier for a method declared in an interface."
来自Java SE 7 JLS(Java 语言规范):“允许但不鼓励为接口中声明的方法冗余指定公共和/或抽象修饰符。”
For Java SE 5.0: "For compatibility with older versions of the Java platform, it is permitted but discouraged, as a matter of style, to redundantly specify the abstract modifier for methods declared in interfaces."
对于Java SE 5.0:“为了与旧版本的 Java 平台兼容,作为一种风格,允许但不鼓励为接口中声明的方法冗余指定抽象修饰符。”