------------android培训、java培训、期待与您交流!--------------
大纲:
一、注解类概述
二、java提供的几个基本注解
三、注解的定义与反射调用
四、注解属性
五、高级注解属性
一、注解类概述
注解相当于一种标记,在程序中加了注解就等于为程序打上了某种标记,没加,则等于没有某种标记,以后,javac编译器,开发工具和其他程序可以用反射来了解你的类及各种元素上有无何种标记,看你有什么标记,就去干相应的事。标记可以加在包,类,字段,方法,方法的参数以及局部变量上。一个注解就是一个类,使用注解,就相当于创建了一个对象。
二、java提供的几个基本注解
1、@SuppressWarnings
示例:
2、@Deprecated
三、注解的定义与反射调用
注解的应用结构图:
注解就相当于一个你的源程序中要调用的一个类,要在源程序中应用某个注解,得先准备好了这个注解类。就像你要调用某个类,得先有开发好这个类。
定义注解类:
public @interface ItheimaAnnotation {
}
对注解类进行调用:
@ItheimaAnnotation
public class AnnotationTest {
public static void main(String[] args) {
if(AnnotationTest.class.isAnnotationPresent(ItheimaAnnotation. class)){
ItheimaAnnotation itheimaAnnotation = AnnotationTest.class .getAnnotation(ItheimaAnnotation. class);
System. out.println(itheimaAnnotation);
}
}
}
修改后的注解类:
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface ItheimaAnnotation {
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ItheimaAnnotation {
}
改为用数组方式设置{ElementType.METHOD,ElementType.TYPE}就不会报错了。
四、注解属性
1、注解属性定义:
一个注解相当于一个胸牌,如果你胸前贴了胸牌,就是黑马的学生,否则,就不是。如果还想区分出是黑马哪个班的学生,这时候可以为胸牌再增加一个属性来进行区分。加了属性的标记效果为:@MyAnnotation(color="red")。
2、定义基本类型的属性和应用属性:
在注解类中增加String color(); 被添加的注解设置属性值:@MyAnnotation(color="red")
3、用反射方式获得注解对应的实例对象后,再通过该对象调用属性对应的方法MyAnnotation a = (MyAnnotation)AnnotationTest.class.getAnnotation(MyAnnotation.class);
System.out.println(a.color());
可以认为上面这个@MyAnnotation是MyAnnotaion类的一个实例对象
4、为属性指定缺省值:String color() default "yellow";
5、value属性:String value() default "zxx";
如果注解中有一个名称为value的属性,且你只想设置value属性(即其他属性都采用默认值或者你只有一个value属性),那么可以省略value=部分,例如:@MyAnnotation("lhm")。
五、高级注解属性
1、数组类型的属性
int[] arrayAttr() default{1,2,3};
@MyAnnotation(arrayAttr={2,3,4})
如果数组属性中只有一个元素,这时候属性值部分可以省略大括号
2、枚举类型的属性
EnumTest.TrafficLamp lamp() ;
@MyAnnotation(lamp=EnumTest.TrafficLamp.GREEN)
3、注解类型的属性
MetaAnnotation annotationAttr() default @MetaAnnotation("xxxx");
@MyAnnotation(annotationAttr=@MetaAnnotation("yyy"))
可以认为上面这个@MyAnnotation是MyAnnotaion类的一个实例对象,同样的道理,可以认为上面这个@MetaAnnotation是MetaAnnotation类的一个实例对象,调用代码如下:
MetaAnnotation ma = myAnnotation.annotationAttr();
System.out.println(ma.value));
示例代码:
public @interface MetaAnnotation {
String value();
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})
public @interface ItheimaAnnotation {
String color() default "blue" ;
String value();
int[] arrayAttr() default {1};
EnumTest.TrafficLamp lamp() default EnumTest.TrafficLamp.RED;
MetaAnnotation annotationAttr() default @MetaAnnotation( "lhm");
Class clazz() default String.class;
}
@ItheimaAnnotation(annotationAttr=@MetaAnnotation ("flx" ),color="red",value= "abc",arrayAttr={1,2,3})
public class AnnotationTest {
@ItheimaAnnotation("xyz" )
public static void main(String[] args) {
if(AnnotationTest.class.isAnnotationPresent(ItheimaAnnotation. class)){
ItheimaAnnotation itheimaAnnotation = AnnotationTest.class .getAnnotation(ItheimaAnnotation. class);
System. out.println(itheimaAnnotation.color());
//结果:red
System. out.println(itheimaAnnotation.arrayAttr().length);
//结果:3
System. out.println(itheimaAnnotation.lamp().nextLamp().name());
//结果:GREEN
System. out.println(itheimaAnnotation.annotationAttr().value());
//结果:flx
System. out.println(itheimaAnnotation.clazz());
//结果:class java.lang.String
}
}
}