|NO.Z.00124|——————————|BigDataEnd|——|Java&反射机制.V04|——|Java.v04|获取其它结构|实现|
一、获取其它结构的实现:获取其它结构信息
方法声明 | 功能介绍 |
Package getPackage() | 获取所在的包信息 |
Class<? super T> getSuperclass() | 获取继承的父类信息 |
Class<?>[] getInterfaces() | 获取实现的所有接口 |
Annotation[] getAnnotations() | 获取注解信息 |
Type[] getGenericInterfaces() | 获取泛型信息 |
二、编程代码
package com.yanqi.task20;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
}
package com.yanqi.task20;
import java.io.Serializable;
@MyAnnotation
public class Student<T, E> extends Person implements Comparable, Serializable {
@Override
public int compareTo(Object o) {
return 0;
}
}
package com.yanqi.task20;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
public class StudentTest {
public static void main(String[] args) throws Exception {
// 获取Student类型的Class对象
Class c1 = Class.forName("com.yanqi.task20.Student");
System.out.println("获取到的包信息是:" + c1.getPackage());
System.out.println("获取到的父类信息是:" + c1.getSuperclass());
System.out.println("-------------------------------------------------");
System.out.println("获取到的接口信息是:");
Class[] interfaces = c1.getInterfaces();
for (Class ct : interfaces) {
System.out.print(ct + " ");
}
System.out.println();
System.out.println("-------------------------------------------------");
System.out.println("获取到的注解信息是:");
Annotation[] annotations = c1.getAnnotations();
for (Annotation at : annotations) {
System.out.print(at + " ");
}
System.out.println();
System.out.println("-------------------------------------------------");
System.out.println("获取到的泛型信息是:");
Type[] genericInterfaces = c1.getGenericInterfaces();
for (Type tt : genericInterfaces) {
System.out.print(tt + " ");
}
System.out.println();
}
}
三、编译打印
D:\JAVA\jdk-11.0.2\bin\java.exe "-javaagent:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=53684:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath E:\NO.Z.10000——javaproject\NO.H.00001.javase\javase\out\production\javase com.yanqi.task20.StudentTest
获取到的包信息是:package com.yanqi.task20
获取到的父类信息是:class com.yanqi.task20.Person
-------------------------------------------------
获取到的接口信息是:
interface java.lang.Comparable interface java.io.Serializable
-------------------------------------------------
获取到的注解信息是:
@com.yanqi.task20.MyAnnotation()
-------------------------------------------------
获取到的泛型信息是:
interface java.lang.Comparable interface java.io.Serializable
Process finished with exit code 0
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
——W.S.Landor
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」