java - 在接口(interface)抽象中标记方法有什么意义吗?

标签 java interface abstract

我将一些方法重构为接口(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/

相关文章:

java - 确保 SpringLayout 不会缩小到一定大小以下

java - 如何在与用户界面线程分开的服务中使用 Androids 位置包?

java - (遗留)TCP 到 ZeroMQ 代理的 ZeroMQ 模式

java - 没有抽象方法的抽象类

java - 用于具体实现抽象类的生成器(Joshua Bloch 风格)?

java - 升级到eclipse juno默认企业版

c# - 为什么要使用显式接口(interface)实现来调用 protected 方法?

Java:将一对元素插入链表

interface - 如何转换为对象可能实现的接口(interface)?

python - 抽象属性(不是属性)?