广州北大青鸟计算机职业培训学校
互联网技术培训、软件技术培训、大数据培训、云计算培训、数据分析培训信息网
当前位置:网站首页 > 软件教程 > Java技术 > 正文

惠州分享Java注释反射实例_北大青鸟IT学校

作者:邓华发布时间:2021-04-06分类:Java技术浏览:679


导读:之前惠州北大青鸟老师给大家分享了Java注释反射的概念,为了让同学们更好的理解Java注释反射,那么下面惠州北大青鸟老师给大家讲讲。

之前惠州北大青鸟老师给大家分享了Java注释反射的概念,为了让同学们更好的理解Java注释反射,那么下面惠州北大青鸟老师给大家讲讲。

实例1

以下代码显示了如何获取特定注释。

import java.lang.annotation.Documented;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
@Documented
@interface Version {  int major();  int minor();
}
@Version(major=1,minor=2)public class Main {  public static void main(String[] argv) {
   Class<Main> c = Main.class;

   Version v = c.getAnnotation(Version.class);    if (v == null) {
     System.out.println("Version annotation is  not  present.");
   } else {      int major = v.major();      int minor = v.minor();
     System.out.println("Version: major=" + major + ", minor=" + minor);
   }

 }
}

上面的代码生成以下结果。


实例2

以下代码显示了如何访问方法的注释。

import java.lang.annotation.Annotation;import java.lang.annotation.Documented;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.reflect.AnnotatedElement;import java.lang.reflect.Method;

@Retention(RetentionPolicy.RUNTIME)
@interface Version {  int major();  int minor();
}

@Version(major = 1, minor = 0)class AccessAnnotation {
 @Version(major = 1, minor = 1)  public void testMethod1() {
 }

 @Version(major = 1, minor = 2)
 @Deprecated  public void testMethod2() {
 }
}public class Main {  public static void main(String[] args) {
   Class<AccessAnnotation> c = AccessAnnotation.class;
   System.out.println("Annotations for class:" + c.getName());
   printAnnotations(c);

   System.out.println("Method annotations:");
   Method[] m = c.getDeclaredMethods();    for (int i = 0; i < m.length; i++) {
     System.out.println("Annotations for method:" + m[i].getName());
     printAnnotations(m[i]);
   }
 }  public static void printAnnotations(AnnotatedElement programElement) {
   Annotation[] annList = programElement.getAnnotations();    for (int i = 0; i < annList.length; i++) {
     System.out.println(annList[i]);      if (annList[i] instanceof Version) {
       Version v = (Version) annList[i];        int major = v.major();        int minor = v.minor();
       System.out.println("Found  Version annotation:  " + "major  =" + major
           + ", minor=" + minor);
     }
   }
 }
}

上面的代码生成以下结果。

Java注释反射实例2.png

实例3

以下代码显示了如何在运行时访问可重复注释的实例。

import java.lang.annotation.Repeatable;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;


@Retention(RetentionPolicy.RUNTIME)
@interface LogHistory {
 Log[] value();
}
@Repeatable(LogHistory.class)
@interface Log {
 String date();
 String comments();
}

@Log(date = "02/01/2014", comments = "A")
@Log(date = "01/22/2014", comments = "B")public class Main {  public static void main(String[] args) {
   Class<Main> mainClass = Main.class;

   Log[] annList = mainClass.getAnnotationsByType(Log.class);    for (Log log : annList) {
     System.out.println("Date=" + log.date() + ", Comments=" + log.comments());
   }

   Class<LogHistory> containingAnnClass = LogHistory.class;
   LogHistory logs = mainClass.getAnnotation(containingAnnClass);
   for (Log log : logs.value()) {
     System.out.println("Date=" + log.date() + ", Comments=" + log.comments());
   }
 }
}

上面的代码生成以下结果。

Java注释反射实例3.png

想了解更多关于Java的资讯,可以来惠州北大青鸟新方舟校区了解一下。

java8.png

Java

标签:惠州计算机JAVA软件开发惠州计算机Java软件开发惠州计算机JAVA培训惠州计算机JAVA软件开发学校惠州计算机Java软件开发培训JAVAJava软件开发北大青鸟IT计算机学校北大青鸟IT软件学校北大青鸟IT学校


Java技术排行
标签列表
网站分类
文章归档
最近发表