A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

Documented,Inherited这几个怎么用啊?

2 个回复

倒序浏览
Documented 。这个元注释也非常容易理解,部分原因是 Documented 是一个标记注释。您应该还记得第 1 部分中曾经提到,标记注释没有成员变量。 Documented 表示注释应该出现在类的 Javadoc 中。在默认情况下,注释 不 包括在 Javadoc 中,如果花费大量时间注释一个类、详细说明未完成的工作、正确完成了什么或者描述行为。
这是代码:
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* Marker annotation to indicate that a method or class
* is still in progress.
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface InProgress { }

希望我的答案对你有用
回复 使用道具 举报
要求为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());
        }
    }
}
希望可以帮到你
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马