我将一些方法重构为接口(interface),它们留下 abstract
接口(interface)声明中的修饰符。我的代码中有这样的内容:
public interface TestAbstractMethod {
public abstract void doSomething();
}
我现在无意中注意到了它,而且我实际上很惊讶该声明没有被标记为无效,它实际上可以编译并工作。
是abstract
出于某种原因允许在接口(interface)中使用修饰符?这会以任何方式改变界面的行为吗?
最佳答案
是的,这是允许的,但 Java 语言规范 §9.4 中不鼓励使用它。 :
Every method declaration in the body of an interface is implicitly abstract, so its body is always represented by a semicolon, not a block.
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 - 在接口(interface)抽象中标记方法有什么意义吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32558823/