要求为API文件@Documented
想要在使用者制作javaDoc文件的同时,也一并将Annotation的信息加入至API文件中,使用java.lang.annotation.Documented
范例:
@Documented
public @interface DocumentedAnnotation{
String hello();
}
使用:
public class DocumentedTest{
@DocumentedAnnotation(hello=”welcome”)
public void method(){
System.out.println(“hello world”);
}
}
子类是否继承父类@Inherited
预设上父类别中的Annotation并不会被继承至子类别中,可以在定义Annotation型态时加上java.lang.annotation.Inherited型态的Anoootation
范例:
@Inherited
@Retention(RententionPolicy.RUNTIME)
public @interface InheritedTest{
String value();
}
父类:
@InheritedTest(“welcome”)
public class parent{
public void method(){
System.out.println(“hello world”);
}
}
子类:
public class Child extends Parent{
public void doSomething(){
System.out.println(“hello world in child”);
}
}
测试类:使用反射机制验证是否发挥作用
pubic class Test{
public static void main(String[] args){
Class<Child> c = Child.class;
if (c.isAnnotationPresent(InheritedTest.class)){
InheritedTest inheritedTest = c.getAnnotation(InheritedTest.class);
System.out.println(inheritedTest.value())
}
}
}
|