黑马程序员技术交流社区
标题:
下面的注释类型怎么应用
[打印本页]
作者:
张建银
时间:
2011-12-27 22:04
标题:
下面的注释类型怎么应用
Documented,Inherited这几个怎么用分别举例1个
作者:
闫江龙
时间:
2011-12-27 23:37
这个技术分是怎么加的啊? ………………
作者:
赵玮
时间:
2011-12-27 23:38
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");
}
作者:
马新乐
时间:
2011-12-27 23:44
要求为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())
}
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2