1、概念:
说明程序的,给计算机看的
定义:注解(Annotation),也叫元数据。一种代码级别的说明。它是JDK1.5之后版本引入的一个特性,与类、接口、枚举是同一个层次。它可以声明在包、类、字段、方法、局部变量、方法参数等前面,用来对这些元素进行说明,注释。
概念描述:
JDK1.5之后用的;
说明程序的;
使用注解:@注解名称
package com.ma.annotation;
/**
* 注解javadoc演示
* @author mayue
* @version 1.0
* @since 1.5
*/
public class AnnoDemo01 {
/**
* 计算两数之和
* @param a 整数
* @param b 整数
* @return 两数之和
*/
public int add(int a, int b){
return a + b;
}
}
2、JDK中预定义的注解
@Override
检测被该注解的方法是否是继承自父类(接口)的
@Deprecated
该注解标识的内容表示已经过时
@SuppressWarnings
压制警告
package com.ma.annotation;
public class AnnoDemo02 {
@Override
public String toString(){
return super.toString();
}
@Deprecated
public void show1(){
// 有缺陷
}
@SuppressWarnings("all")
public void show2(){
// 替代show1()
}
public void demo(){
show1();
}
}
3、自定义注解
格式:元注解+public @interface XXX {}
元注解(描述注解的注解) 1、@Target:描述注解可以作用的位置
public enum ElementType {
/** Class, interface (including annotation type), or enum declaration */
TYPE,
/** Field declaration (includes enum constants) */
FIELD,
/** Method declaration */
METHOD,
/** Formal parameter declaration */
PARAMETER,
/** Constructor declaration */
CONSTRUCTOR,
/** Local variable declaration */
LOCAL_VARIABLE,
/** Annotation type declaration */
ANNOTATION_TYPE,
/** Package declaration */
PACKAGE,
/**
* Type parameter declaration
*
* @since 1.8
*/
TYPE_PARAMETER,
/**
* Use of a type
*
* @since 1.8
*/
TYPE_USE
}
2、@Retention:描述注解保留的阶段(源码、class、运行时)
public enum RetentionPolicy {
/**
* Annotations are to be discarded by the compiler.
*/
SOURCE,
/**
* Annotations are to be recorded in the class file by the compiler
* but need not be retained by the VM at run time. This is the default
* behavior.
*/
CLASS,
/**
* Annotations are to be recorded in the class file by the compiler and
* retained by the VM at run time, so they may be read reflectively.
*
* @see java.lang.reflect.AnnotatedElement
*/
RUNTIME
}
3、@Documented:描述注解是否被抽取到api文档中
4、@Inherited:描述注解是否被子类继承
本质:(一个接口)
public interface MyAnno extends java.lang.annotation.Annotation{}
属性:接口的抽象方法
4、在程序中使用注解
public @interface MyAnno1 {
int age();
String name() default "张三";
// Person per();
// MyAnno2 anno2();
// String[] str();
}
要求:
1.属性的返回值类型(基本数据类型、字符串、枚举、注解、以上类型的数组)
2.定义了属性得赋值(可 default 默认赋值)
package com.ma.annotation;
@MyAnno1(age = 19)
public class Worker {
}
如果只有一个属性需要赋值,并且名叫 value 赋值可省略名称
实战:
定义注解:
package com.ma.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 描述需要执行的类名和方法名
*/
@Target(value = ElementType.TYPE)
@Retention(value = RetentionPolicy.RUNTIME)
public @interface Run {
String className();
String methodName();
}
应用注解:
package com.ma.annotation;
@Run(className = "com.ma.annotationTest.AnnoDemo01",methodName = "show")
public class ReflectTest {
public static void main(String[] args) {
// 获取类的字节码文件
Class<ReflectTest> reflectTestClass = ReflectTest.class;
// 获取上面注解对象
// 该方法其实就是在内存中生成了一个该注解接口的子类对象
Run run = reflectTestClass.getAnnotation(Run.class);
// 调用注解对象中定义的抽象方法,获取返回值
String className = run.className();
System.out.println(className);
String methodNmae = run.methodName();
System.out.println(methodNmae);
}
}