gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:22:48 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com