您的位置:首页 > 编程语言

Annotation注释

2017-11-06 14:01 83 查看
Annotation,即注释,使用注释的方式加入一些程序的信息。
在JDK1.5之后,系统已经建立了三个内建的Annotation类。

内建注解:

@Override 表示方法覆写的正确性
@Deprecated
表示方法不建议使用,程序编译不会出错,但会出现安全警告
@SuppressWarnings 当程序出现安全警告时,可以使用其进行压制,可以同时压制多个安全警告信息,以数组的形式标记

示例:

package study1106;

class Person{
public void saySomething(String info) {//定义一个方法
System.out.println("人类说一些什么   ...");
}
}
/**
* @override 表示方法覆写的正确性
* 方法重写:子类访问权限必须比父类更大;返回值必须相同;方法名必须相同;参数必须相同
* @author SUMMER
*/
public class Student extends Person {
@Override	//覆写父类中的方法可以使用Override作为标记
public void saySomething(String info) {
System.out.println("学生说一些什么");
}
/**
* @Deprecated 表示不建议使用的方法,程序不会出错,但会出现安全警告信息
*/
@Deprecated
public void sayDeprecated() {
System.out.println("不建议使用的方法");
}
}
package study1106;

public class AnnMain {
@SuppressWarnings(value = { "deprecation" })//使用SuppressWarnings压制不建议使用的警告
public static void main(String[] args) {
Student student = new Student() ;
student.sayDeprecated();//使用了不建议使用的方法
}
}


自定义Annotation

可以使用固定格式自定义注解的形式定义自己想要的注解内容

[ 访问权限 ] @interface自定义名称
{

数据类型变量名称
();

}

定义一个可以在程序中直接使用的自定义Annotation

package study1106;
/**
* 自定义一个Annotation
* @author SUMMER
*/
public @interface MyAnnotationOne {

}

还可以向自定义Annotation中设置属性

package study1106;

/**
* 自定义一个可以设置属性的Annotation
* @author SUMMER
*/
public @interface MyAnnotationTwo {
public String value();
}
package study1106;

@MyAnnotationTwo(value = "summer")
public class MyAnnotationTwoTest {

}


可以向自定义Annotation中设置多个属性

public @interface MyAnnotationThree {
public String key();//定义第一个接受属性的方法
public String value();//定义第二个接受属性的方法
}
@MyAnnotationThree(key = "summer" , value = "SUMMER")
public class MyAnnotationThreeTest {

}


可以向自定义Annotation中设置一个数组的形式

public @interface MyAnnotationFour {
public String[] values() ;//定义一个string数组用来接收值
}
@MyAnnotationFour(values = { "SUMMER","summer" })
public class MyAnnotationFourTest {

}


还可以使用default表示设置一个默认内容

public @interface MyAnnotationSix {
public String value () default "summer" ;//使用default定义默认值
}
@MyAnnotationSix
public class MyAnnotationSixTest {

}

还可以使用枚举赋值给Annotation

public enum AnnotationValues {
SUMMER,ZHANG,XIAO,FAN;//定义一个枚举类
}
public @interface MyAnnotationSeven {
public AnnotationValues values() default AnnotationValues.SUMMER;//将枚举类中的内容设置为默认内容
}
@MyAnnotationSeven
public class MyAnnotationSevenTest {

}

Retention和RetentionPolicy

在Annotation中,我们可以使用Retention定义一个Annotation的保存范围,Retention的范围取值具体由RetentionPolicy定义
RetentionPolicy定义了三种取值范围

source 保存在源代码中
.java
class 保存于源代码中,还会在字节码文件中存在
.java&.class
RUNTIME 保存在源码和字节码文件中,运行时可以通过反射获取

@Retention(value = RetentionPolicy.RUNTIME)//定义一个Annotation的保存范围
public @interface MyAnnotationOne {

}
使用反射简单获取类中的注释信息

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(value = ElementType.TYPE)//定义Annotation只能作用在类上
@Retention(value = RetentionPolicy.RUNTIME)//定义Annotation可以在反射中被获取
public @interface MyAnnotationSeven {
public AnnotationValues values() default AnnotationValues.SUMMER;
}
@MyAnnotationSeven
public class MyAnnotationSevenTest {
public static void main(String[] args) {
Class<?> clazz = MyAnnotationSevenTest.class ;
Annotation annotation  = clazz.getAnnotation(MyAnnotationSeven.class);
System.out.println(annotation);
}
}

延伸:Java中内建的Annotation的取值范围

Override @Retention(value = SOURCE),只能在源文件中出现
SuppressWarnings
@Retention(value = SOURCE),只能在源文件中出现
Deprecated @Retention(value = RUNTIME),可以在执行时出现

元注释

之前定义的Annotation可以在任何地方使用,如果要限制使用范围,则可以使用元注释为注释添加注释,改变其属性,主要有@target,@Documented,@Inherited

@Target 定义注释使用的范围,如类,方法,属性等,范围值由ElementType类定义常用字段
@Documented
可以用在任何Annotation上使用,可以通过其为JavaDOC文档设置一些说明信息
@Inherited
表示一个Annotation可以被继承

9afc
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息