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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 张建银 黑马帝   /  2011-12-27 22:04  /  1651 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

Documented,Inherited这几个怎么用分别举例1个

3 个回复

倒序浏览
这个技术分是怎么加的啊? ………………
回复 使用道具 举报
赵玮 黑马帝 2011-12-27 23:38:22
藤椅
Documented:
在默认的情况下在使用javadoc自动生成文档时,注释将被忽略掉。如果想在文档中也包含注释,必须使用Documented为文档注释。
Inherited:
父类的注释并不会被子类继承。如果要继承,就必须加上Inherited注释
package com.xdf.annotation;

@InheritedAnnotation(value="parent")
public abstract class AbstractParent{

        @InheritedAnnotation(value ="Parent abstractMethod")
                public abstract void abstractMethod();
        @InheritedAnnotation(value = "Parent's doExtends")
                public void doExtends(){
                System.out.println("AbstractParent doExtends");
                }

评分

参与人数 1技术分 +1 收起 理由
admin + 1

查看全部评分

回复 使用道具 举报
要求为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())
}
}
}

评分

参与人数 1技术分 +2 收起 理由
admin + 2

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马