java 标记_java – 标记注释与标记接口

在阅读有关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元注释来实现注释?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值