java Docmented注解的作用

Documented也是一种注解,其作用是声明后面的注解是否要在javadoc中输出。

默认情况下,注解是不会在javadoc中出现的。

// @Documented:

@Documented

@Retention(value=RUNTIME)

@Target(value=ANNOTATION_TYPE)

public @interface Documented

{

// 可以是自定义的Annotation类型在javadoc中生成中插入的文档注释信息!

}

———用@Documented在javadoc中显示自定义文档注释:

package com.mldn;

import .lang.annotation.Annotation;

import .lang.annotation.Retention;

import .lang.annotation.RetentionPolicy;

import .lang.annotation.Target;

import .lang.annotation.ElementType;

import java.lang.annotation.Documented;

import java.lang.reflect.Method;

@MyAnnotation(value = “www.k187.com”) // 使用自定义Annotation

public class ReflectAnnotation

{ // 文档注释的格式: /** … */

/**

此方法在对象输出时使用,返回对象信息

*/

@SuppressWarnings(“unchecked”) // 压制警告信息

@Deprecated // 不建议使用

@Override // 覆写的方法

public String toString()

{

// 覆写

return “This is a Override funcation!”;

}

}

// 自定义Annotation类型

@Documented

@Target(value = ElementType.TYPE) // 表示只使用在类型声明上!class,interface( or annotation type),enum

@Retention(value = RetentionPolicy.RUNTIME)

@interface MyAnnotation

{

public String value() default ” “;

}

/* 使用 javadoc -d doc work/ReflectAnnotation.java 名令生成javadoc,里面有如下信息:

@MyAnnotation(value=”www.k187.com”) // 对该类使用了自定义的Annotation,这个Annotation可以在文档中显示中定义的文档注释信息:

public class ReflectAnnotation

extends java.lang.Object

@Deprecated // 该方法上使用了该Annotation

public java.lang.String toString()

已过时。

此方法在对象输出时使用,返回对象信息 // 这是自定义的文档注释信息,

覆盖:

类 java.lang.Object 中的 toString

*/

——-@Inherited参数:

@Documented

@Retention(value=RUNTIME)

@Target(value=ANNOTATION_TYPE)

public @interface Inherited

{

// 表示被修饰的Annotation类型可以在父类与子类之间继承!

}

——-Inherited:

package com.mldn;

import java.lang.annotation.Annotation;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

import java.lang.annotation.ElementType;

import java.lang.annotation.Documented;

import java.lang.annotation.Inherited;

import java.lang.reflect.Method;

public class ReflectAnnotation

{

public static void main(String[] args) throws Exception

{

Class> c = null; // 声明

c = Class.forName(“com.mldn.TestChild”); // 实例化子类的运行时类

if (c.isAnnotationPresent(MyAnnotation.class)) // 如果该类应用了Annotation类型

{

Annotation[] an = c.getAnnotations(); // 获取当前类上应用的Annontation

for (Annotation a : an)

System.out.println(“子类继承的Annotation: ” + a); // 输出

}

}

}

class TestChild extends Test // 子类

{

// 将继承Test的Annotation

}

@MyAnnotation(value = “www.k187.com”) // 使用自定义Annotation

class Test // 父类

{ // 文档注释的格式: /** … */

/**

此方法在对象输出时使用,返回对象信息

*/

@SuppressWarnings(“unchecked”) // 压制警告信息

@Deprecated // 不建议使用

@Override // 覆写的方法

public String toString()

{

// 覆写

return “This is a Override funcation!”;

}

}

// 自定义Annotation类型

@Documented

@Inherited // 表示这个自定义的Annotation可以由父类继承给子类

@Target(value = ElementType.TYPE) // 表示只使用在类型声明上!class,interface( or annotation type),enum

@Retention(value = RetentionPolicy.RUNTIME)

@interface MyAnnotation

{

public String value() default ” “;

}

/*

ubuntu@xu-desktop:~$ java com.mldn.ReflectAnnotation

子类继承的Annotation: @com.mldn.MyAnnotation(value=www.k187.com)

*/

// 反射机制 对 Annotation的使用很重要! EjB3.0中大量使用Annotation!

声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 智乐兔
转载请注明:转自《java Docmented注解的作用
本文地址:https://www.zhiletu.com/archives-151.html
关注公众号:智乐兔

赞赏

wechat pay微信赞赏alipay pay支付宝赞赏

上一篇
下一篇

相关文章

在线留言

你必须 登录后 才能留言!

在线客服
在线客服 X

售前: 点击这里给我发消息
售后: 点击这里给我发消息

智乐兔官微