注解是JDK1.5有的java
应用注解,进行开发和应用:数组
注解至关于一个特殊的类哦!ide
首先定义一个注解的类 @interface toov5测试
而后应用这个注解 @Aspa
而后对这个类进行反射调用设计
一个注解的生命周期有三个阶段:3d
一、 RetentionPolicy.RUNTIMEcode
2 、 RetentionPolicy.SOURCE对象
三、 RetentionPolicy.CLASSblog
分别对应Java源文件 calss文件(经过类加载器调用进内存就没有了,好比 @Override等) 内存中的字节码 这三个阶段均可以将注解去掉。 设计时候能够进行声明
总结: 注解用在哪一个阶段、什么类型(方法、类、包、注解、构造方法等)上面。
元注解以及其枚举属性值不用记,直接看java.lang.annotation包下面的类
作一个注解:
@Retention(RetentionPolicy.RUNTIME) //为注解服务的 元注解 保留到运行期间 //@Target(ElementType.METHOD) //只能放在Method上不能放在类上 @Target( {ElementType.TYPE, ElementType.METHOD}) //数组里面 类 方法 public @interface toov5 { }
使用它:
@toov5 public class test1 { public static void main(String[] args) { //玩字節碼,看看有注解沒 注解是否在場 boolean annotationPresent = test1.class.isAnnotationPresent(toov5.class); if (annotationPresent){ //若是有这个注解 toov5 annotation = test1.class.getAnnotation(toov5.class); //获得这个对象 System.out.println(annotation); } } }
注解的属性至关于一个胸牌,好比实验中学学生。若是再想细致的区分,那么能够经过这个胸牌再加一个属性区分。
@MyAnnotation(color = "red")
能够有默认值:
@Retention(RetentionPolicy.RUNTIME) //为注解服务的 元注解 保留到运行期间 //@Target(ElementType.METHOD) //只能放在Method上不能放在类上 @Target( {ElementType.TYPE, ElementType.METHOD}) //数组里面 类 方法 public @interface toov5 { String color() default "red"; //抽象的而且是public的 指定一个属性默认值 String value(); }
测试:
@toov5(value = "test") // 建立这个注解的实例对象时候须要给属性赋值 public class test1 { public static void main(String[] args) { //玩字節碼,看看有注解沒 注解是否在場 boolean annotationPresent = test1.class.isAnnotationPresent(toov5.class); if (annotationPresent){ //若是有这个注解 toov5 annotation = test1.class.getAnnotation(toov5.class); //获得这个对象 System.out.println(annotation.color()+"-----"+annotation.value()); //打印这个注解属性 取值时候仍是以方法调用的形式进行调用 } } }
属性复杂一些的好比 数组类型的:
注解:
@Retention(RetentionPolicy.RUNTIME) //为注解服务的 元注解 保留到运行期间 //@Target(ElementType.METHOD) //只能放在Method上不能放在类上 @Target( {ElementType.TYPE, ElementType.METHOD}) //数组里面 类 方法 public @interface toov5 { String color() default "red"; //抽象的而且是public的 指定一个属性默认值 String value(); int[] arrayAttr() default {1,6}; }
使用:
@toov5(value = "test", arrayAttr = {1,3,6}) // 建立这个注解的实例对象时候须要给属性赋值 public class test1 { public static void main(String[] args) { //玩字節碼,看看有注解沒 注解是否在場 boolean annotationPresent = test1.class.isAnnotationPresent(toov5.class); if (annotationPresent){ //若是有这个注解 toov5 annotation = test1.class.getAnnotation(toov5.class); //获得这个对象 System.out.println(annotation.color()+"-----"+annotation.value()); //打印这个注解属性 取值时候仍是以方法调用的形式进行调用 System.out.println(annotation.arrayAttr().length); } } }
结果:
属性的值可使注解的!
另外写一个注解:
public @interface toov5Annotation { String value(); }
注解中引用这个注解:
@Retention(RetentionPolicy.RUNTIME) //为注解服务的 元注解 保留到运行期间 //@Target(ElementType.METHOD) //只能放在Method上不能放在类上 @Target( {ElementType.TYPE, ElementType.METHOD}) //数组里面 类 方法 public @interface toov5 { String color() default "red"; //抽象的而且是public的 指定一个属性默认值 String value(); int[] arrayAttr() default {1,6}; //返回注解 toov5Annotation toov5Annotation() default @toov5Annotation("toov5"); //默認值 }
测试:
@toov5(value = "test", arrayAttr = {1,3,6} , toov5Annotation = @toov5Annotation("ttttttest")) // 建立这个注解的实例对象时候须要给属性赋值 public class test1 { public static void main(String[] args) { //玩字節碼,看看有注解沒 注解是否在場 boolean annotationPresent = test1.class.isAnnotationPresent(toov5.class); if (annotationPresent){ //若是有这个注解 toov5 annotation = test1.class.getAnnotation(toov5.class); //获得这个对象 System.out.println(annotation.color()+"-----"+annotation.value()); //打印这个注解属性 取值时候仍是以方法调用的形式进行调用 System.out.println(annotation.arrayAttr().length); System.out.println(annotation.toov5Annotation().value()); } } }
结果:
属性也但是是枚举类的!
略
也能够是class! 略