如果我们有 @Override 为什么没有 @Implementation 或 @Implements`?
我很好奇为什么我们有 @Overrides 注释,但接口没有类似的习惯用法(例如 @Implements 或 @Implementation)。这似乎是一个有用的功能,因为您可能需要将正在实现的接口作为注释的值。
这是一个有意识的决定还是只是一个被忽视的决定?
我发现这个问题 ,但它似乎没有讨论为什么没有创建单独的注释。
I'm curious as to why we have the @Overrides annotation, but there is not a similar idiom for interfaces (such as @Implements or @Implementation). It seems like it'd be a useful feature, as you could require the interface you are implementing to be a value of the annotation.
Was this a conscious decision or just an overlooked one?
I found this question, but it doesn't seem to discuss why there wasn't a separate annotation created.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在大多数情况下,您不需要这样的注释,因为如果您不实现该接口,您将收到编译时错误。唯一的情况是该类是抽象类。在那里它可能很有用。但话又说回来,你有@Override,所以新的注释可能是无用的。
In most cases you don't need such an annotation, because if you don't implement the interface you will receive a compile-time error. The only case is if the class is
abstract
. There it can be useful. But then again you have the@Override
, so a new annotation is probably useless.我认为当你实现一个
接口
(例如public class AClass Implements AInterface
)时,你正在使用该关键字implements
,这足以让要知道,即使您使用@Override,该方法实际上也是抽象方法的实现。I think when you are implementing an
interface
(e.g.public class AClass implements AInterface
), you are using that keywordimplements
which is enough info to let know that even if you are using@Override
, the method is actually an implementation of an abstract method.@Override
注解只是编译器的一个标记。它的存在是为了让编译器可以在开发人员的意图可能被混淆的地方生成警告。 javadoc 内容如下:任何 @Implements 注释可能只提供相同的功能,因此其实用性仅限于编译器。
The
@Override
annotation is merely a marker for the compiler. It exists so that the compiler can generate warnings where the developer's intentions are perhaps confused. The javadoc reads as follows:Any
@Implements
annotation would probably only provide the same function, so its utility would be limited to the compiler.