注解
* 它不是注释 注释是程序员写的,给程序员看
* 注解,用于描述程序如何运行及在什么阶段来运行。
* 注解现在在实际开发中,最大的功能是用于替换配置文件。
* 注解是jdk1.5的新特性
* 可以通过反射来让注解具有功能。
* 注解 @xxxx
1.1、自定义注解
1、JDK中的三个基本的注解:
a、@Override:检查子类确实是覆盖了父类的方法。
b、@Deprecated:说明已经过时了。
c、@SuppressWarnings("deprecation"):抑制程序中的警告。
public class Animal {
public void eat() {
}
}
public class Dog extends Animal{
@Override
@Deprecated
public void eat() {
super.eat();
}
}
2、自定义注解的语法:(肉体)
* 注解的本质
* 声明一个注解 @interface 注解名{}
* public @interface MyAnnotation{}
* 注解它的本质就是一个接口,这个接口需要继承 Annotation接口。
分析注解中的成员
注解本质上就是接口,接口中可以有属性方法
属性 : 例:int age();
关于注解的属性类型可以有哪些?
1.基本类型
2.String
3.枚举类型
4.注解类型
5.Class类型
6.以上类型的一维数组
定义注解
public @interface MyAnnotation {
int age() default -1;
String name() default "xiaohei";
Class getClss();
String[] values();
}
@MyAnnotation(age=1,name="小明",values = { "1","2" })
public class Dog extends Animal{
@Override
@Deprecated
public void eat() {
super.eat();
}
@MyAnnotation(age=1,name="小明",values = { "1","2" })
public void play() {
}
}
总结:注解就是在你的程序代码中的某个位置加了一个标记而已。
3、注解的反射:(灵魂)
a、反射注解类
public interface AnnotatedElement
<T extends Annotation> T getAnnotation(Class<T> annotationType):得到指定类型的注解引用。没有返回null。
Annotation[] getAnnotations():得到所有的注解,包含从父类继承下来的。
Annotation[] getDeclaredAnnotations():得到自己身上的注解。
boolean isAnnotationPresent(Class<? extends Annotation> annotationType):判断指定的注解有没有。
如果:Class.isAnnotationPresent(MyAnnotation.class):判断类上面有没有@MyAnnotation.class注解;
Method.isAnnotationPresent(MyAnnotation.class):判断方法上面有没有MyAnnotation.class注解。
import java.lang.reflect.Method;
public class Test1 {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
Class clss=Dog.class;
Method[] methods = clss.getMethods();
if(clss.isAnnotationPresent(MyAnnotation.class)) {
System.out.println(clss.getSimpleName()+"存在MyAnnotation注解");
}else {
System.out.println(clss.getSimpleName()+"不存在MyAnnotation注解");
}
for (Method m:methods) {
MyAnnotation annotation = m.getAnnotation(MyAnnotation.class);
if(annotation!=null) {
System.out.println(m.getName()+"存在MyAnnotation注解");
}else {
System.out.println(m.getName()+"不存在MyAnnotation注解");
}
}
}
}
4、元注解
a、自定义的注解的存活范围(生命周期):默认是CLASS。
什么是元注解:
只能用在注解上的注解叫做元注解。(即:用于修饰注解的注解)
* @Retention:作用。改变自定义的注解的存活范围。
RetentionPolicy:
SOURCE
CLASS
RUNTIME
* @Target:作用,指定该注解能用在什么地方。
ElementType:
TYPE:
METHOD:
FIELD:
ANNOTATION_TYPE
* @Documented:作用,使用了@MyAnnotation的注解的类,如果@MyAnnotation注解上面有@Documented注解,那么使用了@MyAnnotation的注解的类的API文档中会出现@MyAnnotation的身影。
* @Inherited:作用,说明该注解可以被继承下去。
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
public class Test1 {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
Class clss=Dog.class;
Method[] methods = clss.getMethods();
if(clss.isAnnotationPresent(MyAnnotation.class)) {
MyAnnotation annotation = (MyAnnotation) clss.getAnnotation(MyAnnotation.class);
String name = annotation.name();
System.out.println(name);
}else {
}
for (Method m:methods) {
MyAnnotation annotation = m.getAnnotation(MyAnnotation.class);
if(annotation!=null) {
String name=annotation.name();
System.out.println(name);
}else {
}
}
}
}