java教程之java注解annotation使用方法
1.概述 注解可以定义到方法上,类上,一个注解相当与一个类,就相当于实例了一个对象,加上了注解,就相当于加了一个标志。 常用的注解: 复制代码 代码如下: import java.util.ArrayList; import java.util.List; public class annotationDemo { /* * 对于集合,如果没有指定存储的类型,那么就会有安全警告, * 如果不想提示安全警告的话,那么就所在类或者方法上添加@SuppressWarnings(参数) */ @SuppressWarnings("unchecked") public static void main(String[] args) { List list=new ArrayList(); } } 2.自定义注解 1.格式 复制代码 代码如下: import java.lang.annotation.Retention; importjava.lang.annotation.RetentionPolicy; //定义此注解保留在字节码中 @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation { } @MyAnnotation // 应用定义的注解类 public class ApplyMyAnnotation { public static void main(String[] args) { if (ApplyMyAnnotation.class.isAnnotationPresent(MyAnnotation.class)) {// 判断此类上是否存在指定的注解类 MyAnnotation annotation= (MyAnnotation) ApplyMyAnnotation.class .getAnnotation(MyAnnotation.class); System.out.println(annotation); } } } 2.声明周期 格式:例如:@Retention(RetentionPolicy.CLASS) 3.指定目标 复制代码 代码如下: //定义此注解保留在字节码中 @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD,ElementType.TYPE})//可以定义在方法上和类上接口,表示类型 public @interface MyAnnotation { } @MyAnnotation // 应用定义的注解类 public class ApplyMyAnnotation { @MyAnnotation//定义在方法上 public static void main(String[] args) { if (ApplyMyAnnotation.class.isAnnotationPresent(MyAnnotation.class)) {// 判断此类上是否存在指定的注解类 MyAnnotation annotation = (MyAnnotation) ApplyMyAnnotation.class .getAnnotation(MyAnnotation.class); System.out.println(annotation); } } } 3.为注解添加属性 复制代码 代码如下: import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.*; //定义此注解保留在字节码中 @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation { String value() ; String Color()default "red";//设置默认值是"red" } @MyAnnotation("java") public class ApplyMyAnnotation { public static void main(String[] args) { /** * 这是获得类上的注解,也可以获得方法上的注解,下面就以获得类上的注解为例 */ if (ApplyMyAnnotation.class.isAnnotationPresent(MyAnnotation.class)) {// 判断此类上是否存在指定的注解类 MyAnnotation annotation = (MyAnnotation) ApplyMyAnnotation.class .getAnnotation(MyAnnotation.class); System.out.println("value="+annotation.value()); System.out.println("Color="+annotation.Color()); } } } 结果: 复制代码 代码如下: /*枚举类*/ public enum Week{ SUN,MON; } /** * 注解类 */ public @interface annotationText { String value(); } import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.*; //定义此注解保留在字节码中 @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation { String value() ; String Color()default "red";//设置默认值是"red" Week week() default Week.MON;//枚举类型 int [] array() default {1,2,3};//数组类型 annotationText annotation() default @annotationText("MY");//注解类型 Class classDemo() default Integer.class;//Class类型 } @MyAnnotation(value="java",Color="green",week=Week.SUN,array=5,annotation=@annotationText("YOU"),classDemo=String.class)//数组array={4,5,6} public class ApplyMyAnnotation { public static void main(String[] args) { /** * 这是获得类上的注解,也可以获得方法上的注解,下面就以获得类上的注解为例 */ if (ApplyMyAnnotation.class.isAnnotationPresent(MyAnnotation.class)) {// 判断此类上是否存在指定的注解类 MyAnnotation annotation= (MyAnnotation) ApplyMyAnnotation.class .getAnnotation(MyAnnotation.class); System.out.println("value="+annotation.value()); System.out.println("Color="+annotation.Color()); System.out.println("week="+annotation.week()); System.out.println("array长度="+annotation.array()。length); System.out.println("注解类型值="+annotation.annotation()。value()); System.out.println("Class类型值="+annotation.classDemo()); } } } 结果: 复制代码 代码如下: value=java Color=green week=SUN array长度=1 注解类型值=YOU Class类型值=classjava.lang.String 4.Method上的注解 复制代码 代码如下: importjava.lang.annotation.Retention; importjava.lang.annotation.RetentionPolicy; /** *注解类 */ @Retention(RetentionPolicy.RUNTIME) public @interface annotationText{ Stringvalue(); } publicclassApplyMyAnnotation{ publicstaticvoidmain(String[]args)throwsException{ Methodmethodshow=ApplyMyAnnotation.class.getMethod("show"); annotationTextanno=methodshow.getAnnotation(annotationText.class); System.out.println(anno.value()); } @annotationText("java") publicvoidshow(){ System.out.println("hello"); } } 结果:java 您可能感兴趣的文章:
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |