在您定义Annotation型态后并使用于程序代码上时,预设上父类别中的Annotation并不会被继承至子 类别中,您可以在定义 Annotation时加上java.lang.annotation.Inherited的Annotation,这让您定义的Annotation型 别被继承下来。
例如:
package onlyfun.caterpillar; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Inherited; @Retention(RetentionPolicy.RUNTIME) @Inherited public @interface Debug { String value(); String name(); }
在下面这个程序中使用它:
package onlyfun.caterpillar; public class SomeObject { @Debug( value = "unit", name = "debug1" ) public void doSomething() { // .... } }
如果您有一个类别继承自SomeObject,则理想上@Debug也会被继承下来,不过事实上Inherited在Sun JDK 5.0中还没有作用。