0

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.

4

3 回答 3

4

在大多数情况下,您不需要这样的注释,因为如果您不实现接口,您将收到编译时错误。唯一的情况是如果类是abstract. 在那里它可能很有用。但是话又说回来了@Override,所以一个新的注释可能没用。

于 2011-11-30T14:54:54.640 回答
0

我认为当您实现interface(例如public class AClass implements AInterface)时,您正在使用该关键字,该关键字implements足以让您知道即使您正在使用@Override该方法,该方法实际上也是抽象方法的实现。

于 2011-11-30T15:00:39.873 回答
0

@Override注释只是编译器的标记。它的存在是为了让编译器可以在开发人员的意图可能被混淆的地方生成警告。javadoc 内容如下:

表示方法声明旨在覆盖超类中的方法声明。如果一个方法被这个注解类型注解但没有覆盖超类方法,编译器需要生成一个错误信息。

任何@Implements注释都可能只提供相同的功能,因此它的实用性将仅限于编译器。

于 2011-11-30T15:04:03.763 回答