在接口抽象中标记方法是否有意义?
Is there a point of marking a method in interface abstract?
我已经将一些方法重构为一个接口,它们在接口声明中留下了 abstract
修饰符。我的代码中有这样的内容:
public interface TestAbstractMethod {
public abstract void doSomething();
}
我现在无意中注意到了它,我真的很惊讶声明没有被标记为无效,它实际上可以编译和工作。
是否出于某种原因在界面中允许使用 abstract
修饰符?这会以任何方式改变界面的行为吗?
是的,这是允许的,但在 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.
我已经将一些方法重构为一个接口,它们在接口声明中留下了 abstract
修饰符。我的代码中有这样的内容:
public interface TestAbstractMethod {
public abstract void doSomething();
}
我现在无意中注意到了它,我真的很惊讶声明没有被标记为无效,它实际上可以编译和工作。
是否出于某种原因在界面中允许使用 abstract
修饰符?这会以任何方式改变界面的行为吗?
是的,这是允许的,但在 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.