标记注释与标记界面 [英] Marker Annotation vs Marker Interface

查看:131
本文介绍了标记注释与标记界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读Marker接口时,我偶然发现了以下网站:项目37:使用标记界面定义类型


根据Joshua Bloch的说法,Marker接口相对于Marker注释有两个优点。

While reading about Marker interfaces I stumbled upon the following site : Item 37: Use marker interfaces to define types
Here, according to Joshua Bloch there are two advantages of Marker interfaces over the Marker annotations.



  1. 标记接口定义由
    实例标记的类实现的类型;标记注释没有。这个
    类型的存在允许你在编译时捕获错误,如果你使用了标记注释,你就不能在运行时捕获

  1. 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.

标记接口相对于标记注释的另一个优点是
,它们可以更精确地定位。如果注释类型是使用目标 ElementType.TYPE 声明的
,则它可以应用于任何
类或接口。假设您有一个仅适用于特定接口实现的标记
。如果你将它定义为
作为标记接口,你可以让它将唯一的接口扩展到它适用的
,保证所有标记类型也是唯一接口的
子类型。是适用的。

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.


好的,第一点被理解但我不确定如果我正确理解第二点:

OK, First point is understood but I'm not sure if I understand the 2nd point correctly:


如果使用目标 ElementType.TYPE ,它可以应用于任何类或接口。

If an annotation type is declared with target ElementType.TYPE, it can be applied to any class or interface.

同样,如果我有一个标记接口,那么它也可以应用于任何类或接口。是不是对标记注释和标记界面说了同样的话? 如何更精确地定位标记界面?

Likewise, if I have a marker interface then that too can be applied to any class or interface. Isn't it saying the same thing about marker annotations and marker interfaces? How can a marker interface be targeted more precisely?

第二点还提到:


您可以让[标记接口]扩展适用的唯一接口,保证所有标记类型也是适用的唯一接口的子类型。

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 meta-annotation

Can't you also achieve this with annotations, by using the @Inherited meta-annotation?

推荐答案


怎么样?标记界面更准确地定位?

你说两者都可以应用于任何类型都是正确的。通过更准确地定位,作者意味着您可以添加其他限制,以便可以将标记界面应用于哪些特定类型。无法对注释添加相同的精确限制:如果注释仅限于 ElementType.TYPE ,则它始终可以应用于所有类型。

You are correct that both could be applied to any type. By "targeted more precisely" the author means that you can add additional restrictions to which specific types a marker interface can be applied to. It is not possible to add the same precise restrictions to annotations: If an annotation is restricted to ElementType.TYPE, then it can always be applied to all types.

第二点的另一部分详细介绍了如何添加这些限制。如果你有一个标记界面,你可以让它扩展另一个界面(作者称之为唯一界面),如下所示:

The other part of the 2nd point goes into details how you can add those restrictions. If you have a marker interface, you can let it extend another interface (which the author calls the sole interface) like this:

interface Marker extends Foo { }

此标记现在只能应用于实现 Foo 的类型。

The marker can now only be applied to types which implement Foo.


使用 @Inherited meta-annotation

Can't you also achieve this with annotations, by using the @Inherited meta-annotation?

不, @Inherited 元注释仅表示注释类的任何子类型都将被视为具有相同的注释。它不对可以应用注释的类型施加任何限制。

No, the @Inherited meta-annotation only means that any subtype of an annotated class will be treated as if it also had the same annotation. It does not impose any restrictions to which types the annotation can be applied to.

这篇关于标记注释与标记界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆