在阅读有关Marker接口的文章时,我偶然发现了以下网站:Item 37: Use marker interfaces to define types
根据Joshua Bloch的说法,Marker接口相对于Marker注释有两个优点.
Marker interfaces define a type that is implemented by instances of
the marked class; marker annotations do not. The existence of this
type allows you to catch errors at compile time that you couldn’t
catch until runtime if you used a marker annotation.
Another advantage of marker interfaces over marker annotations is
that they can be targeted more precisely. If an annotation type is
declared with target ElementType.TYPE, it can be applied to any
class or interface. Suppose you have a marker that is applicable
only to implementations of a particular interface. If you define it
as a marker interface, you can have it extend the sole interface to
which it is applicable, guaranteeing that all marked types are also
subtypes of the sole interface to which it is applicable.
好的,第一点被理解,但我不确定我是否正确理解第二点:
If an annotation type is declared with target ElementType.TYPE, it can be applied to any class or interface.
同样,如果我有一个标记接口,那么它也可以应用于任何类或接口.是不是对标记注释和标记界面说了同样的话?如何更精确地定位标记界面?
第二点还提到:
you can have [the Marker Interface] extend the sole interface to which it is applicable, guaranteeing that all marked types are also subtypes of the sole interface to which it is applicable.
您是否也可以通过使用@Inherited元注释来实现注释?