小典
只是没有误会。 请与java.lang.annotation.Inherited联系。 这是评论的评论。 这意味着,被注释的类的子类将被视为具有与父类相同的注释。
范例
考虑以下两个评论。
@Inherited
@target(elementtype.type ) )。
@ retention (retention policy.runtime ) )。
公共@ interfaceinheritedannotationtype {
}
和
@target(elementtype.type ) )。
@ retention (retention policy.runtime ) )。
公共@ interfaceuninheritedannotationtype {
}
如果这样评论了三个类:
@UninheritedAnnotationType
class A {
}
@InheritedAnnotationType
class B extends A {
}
类c extends b {
}
执行此代码
system.out.println (newa (.getclass ).get annotation (inheritedannotationtype.class ) );
system.out.println (newb (.getclass ).get annotation (inheritedannotationtype.class ) );
system.out.println (newc (.getclass ).get annotation (inheritedannotationtype.class ) );
system.out.println (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
system.out.println (newa (.getclass ).get annotation (uninheritedannotationtype.class ) );
system.out.println (newb (.getclass ).get annotation (uninheritedannotationtype.class ) );
system.out.println (newc (.getclass ).get annotation (uninheritedannotationtype.class ) );
这样的结果会被打印出来。 取决于注释的包。
空值
@InheritedAnnotationType () )
@InheritedAnnotationType () )
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ u
@UninheritedAnnotationType ()
空值
空值
正如您所看到的,UninheritedAnnotationType不能继承,但C继承注释InheritedAnnotationType来自b。
我不知道和这个有什么关系。
2020-10-18