为什么@FunctionalInterface具有RUNTIME保留? [英] Why does @FunctionalInterface have a RUNTIME retention?
问题描述
如果使用此注释类型注释类型,则生成错误消息所需的编译器为
,除非......
If a type is annotated with this annotation type, compilers are required to generate an error message unless ...
为什么不是 SOURCE
或 CLASS
足够,像对于 @Override
。
Why isn't SOURCE
or CLASS
enough, like for @Override
.
推荐答案
@ FunctionalInterface
注释有两个目的。关于编译器及其必须生成的错误,确实足以在此方面具有 SOURCE
RetentionPolicy
它只影响用 @FunctionalInterface
注释的类。
The @FunctionalInterface
annotation serves two purposes. Regarding the compiler and the error it has to generate it would be indeed enough to have a SOURCE
RetentionPolicy
as in this regard it only affects the very class annotated with @FunctionalInterface
.
然而,它有第二个目的,记录事实使用这个接口
作为功能接口确实是有意的,并且以这种方式使用它的可能性不仅仅是巧合,例如可比较的
不打算以这种方式使用。
However, it has a second purpose, documenting the fact that using this interface
as a functional interface is indeed intended and the possibility to use it this way not just a coincidence like with, e.g. Comparable
which is not intended to be used that way.
因此它用注释@Documented
并且具有最大 RetentionPolicy
以实现第二个目的。
Therefore it is annotated with @Documented
and has the maximum RetentionPolicy
to fulfill the second purpose.
这篇关于为什么@FunctionalInterface具有RUNTIME保留?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!