转自:
Java.lang.Class类 getAnnotation方法有什么功能呢?
下文讲述Class类中的getAnnotation方法的功能,如下所示:
getAnnotation方法的功能
java.lang.Class.getAnnotation(Class<T> annotationClass) 方法方法的功能
获取注解类型,此方法使用对象的方式返回类
getAnnotation方法的语法
语法 public T getAnnotation(Class<T> annotationClass) 参数 annotationClass:待获取注解的类型 返回值 返回注解类的指定对象
例:
getAnnotation方法的示例分享
package com.java265.other; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import org.junit.Test; import com.java.other.other.AnnotationJava265; @AnnotationJava265(a = "java", b = "教程网") public class other { /** * java265.com java.lang.Class 测试示例分享 */ @Test public void test() { Class c = other.class; System.out.println("class: " + c.toString()); System.out.println("Annotation of myClass: " + c.getAnnotation(AnnotationJava265.class)); } @Retention(RetentionPolicy.RUNTIME) @interface AnnotationJava265 { public String a(); public String b(); } } -------运行以上代码,将输出以下信息----- class: class com.java.other.other Annotation of myClass: @com.java.other.other$AnnotationJava265(a="java", b="\u6559\u7a0b\u7f51")