一、注解
1、注解概述
(1)注解介绍
- 注解(Annotation)不是程序本身,可以对程序作出解释说明
- 注解可以被其他程序(比如编译器等)读取(注解信息处理流程,是注解和注释的重大区别)。
(2)注解的语法
@注释名
后面还可以添加一些参数,比如
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
- @符号的作用:标示这是一个注解。
(3)注解的使用
可以在package、class、method、field等上面使用,相当于给它们添加了额外的信息。
2、常见注解
(1)@Override
重写注解
定义在 java.lang.Override
中,此注释只适用于修饰方法,表示一个方法声明打算重写其他类中的另一个方法声明。
(2)@Deprecated
过时注解
定义在 java.lang.Deprecated
中,此注释可用于修饰方法、属性、类,表示不建议使用被注释内容,通常是因为这些内容过时或者很危险或者存在更好的选择。
(3)@SuppressWarnings
警告注解
定义在java.lang.SuppressWarnings
中,用来抑制编译时的警告信息。这个注释必须要添加参数才能使用,这些参数都是已经定义好了的,可以直接使用
参数如:
deprecation
:使用了过时的类或者方法的警告;unchecked
:执行了未检查的转换的警告,如使用集合时未指定泛型;path
:在类路径、源文件路径等中有不存在路径的警告;fallthrough
:当在switch语句使用发生case穿透;serial
:当在可序列化的类上缺少 serialVersionUID 定义是的警告finally
:任何finally子句中不能完成时的警告;all
:关于以上所有情况的警告。
//例如
@SuppressWarnings("unchecked")
二、注解示例
1、注解说明
(1)@interface
使用@interface自定义注解时,自动继承了java.lang.annotation.Annotation
接口
(2)元注解(meta-annotation)
① @Target:用于描述注解的使用范围,即被描述的注解可以使用在什么地方。
- PACKAGE:用于描述包
- TYPE:用于类、接口、枚举、Annotation类型
- CONSTRUCCTOR:用于描述构造器
- FIELD:用于描述字段(域)
- METHOD:用于描述方法
- LOCAL_VARIABLE:用于描述局部变量
- PARAMETER:用于描述参数
例如
@Target(value=ElementType.TYPE)
② @Retention:表示需要在什么级别保存该注释信息,用于描述注解的生命周期。
-SOURCE:在源文件中有效(即源文件保留)
- CLASS:在class文件中有效(即class保留)
- RUNTIME:在运行时有效(即运行时保留),为Runtime可以被反射机制读取
③ @Documented:
④ @Inherited:
元注解的作用是负责注解其他注解
2、注解示例
(1)示例1:在类上面自定义注解
① 自定义注解接口类
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface MyAnnotation {
public String name();
public String value();
}
@Retention(RetentionPolicy.RUNTIME)
设置注解在运行时有效
@Target(ElementType.TYPE)
设置注解类型,表示是在类上面注解还是方法上注解还是其他上面注解等。
② 定义一个普通类
@MyAnnotation(name = "name",value = "value")
public class ClassDemo {
}
③ 主函数类
public class TestDemo {
public static void main(String[] args) {
Class classDemo = ClassDemo.class;
Annotation anno = classDemo.getAnnotation(MyAnnotation.class);
if(anno instanceof MyAnnotation) {
MyAnnotation myAnnotation = (MyAnnotation)anno;
System.out.println(myAnnotation.name() + " " + myAnnotation.value());
}
}
}
(2)示例2:在方法上注解
① 自定义注解接口类
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAnnotation {
public String name();
public String value();
}
② 定义一个普通类
public class ClassDemo {
@MyAnnotation(name = "hello",value = "world")
public void eat(){
}
}
③ 主函数类
public class TestDemo {
public static void main(String[] args) throws NoSuchMethodException, SecurityException {
Class classDemo = ClassDemo.class;
Method method = classDemo.getMethod("eat", null);
MyAnnotation anno = method.getAnnotation(MyAnnotation.class);
if(anno instanceof MyAnnotation) {
MyAnnotation myAnnotation = (MyAnnotation)anno;
System.out.println(myAnnotation.name() + " " + myAnnotation.value());
}
}
}
(3)示例3:对参数的注解
① 自定义注解类
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface Demo1 {
public String value();
}
② 定义一个普通类
public class FromDemo1 {
public void methodDemo(@Demo1("张三")String value) {
}
}
③ main函数类
public class TestDemo1 {
public static void main(String[] args) throws NoSuchMethodException, SecurityException {
Class clazz = FromDemo1.class;
//获取方法对象
Method method = clazz.getMethod("methodDemo", String.class);
//使用method对象获取Parameter的注解
Annotation[][] annotations = method.getParameterAnnotations();
for(Annotation[] anno: annotations) {
for(Annotation a: anno) {
//判断
if(a instanceof Demo1) {
Demo1 demo1 = (Demo1)a;
System.out.println(demo1.value());
}
}
}
}
}
(4)示例4:对字段的注解
① 自定义注解类
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Demo2 {
public String name() default "李四";
}
② 普通类
public class FromDemo2 {
@Demo2()
public String name = "李四";
}
③ main函数类
public class TestDemo2 {
public static void main(String[] args) throws NoSuchFieldException, SecurityException {
Class<FromDemo2> clazz = FromDemo2.class;
Field field = clazz.getField("name");
Annotation[] annotations = field.getAnnotations();
for(Annotation anno: annotations) {
if(anno instanceof Demo2) {
Demo2 demo2 = (Demo2)anno;
System.out.println(demo2.name());
}
}
}
}