首页 > 编程语言 > 详细

JavaSE:反射机制 - 获取其他结构信息

时间:2021-06-16 14:33:02      阅读:9      评论:0      收藏:0      [点我收藏+]

1.  

Package getPackage() 获取所在的包信息
Class<? super T>getSuperclass() 获取继承的父类信息
Class<?>[] getInterfaces() 获取实现的所有接口
Annotation[] getAnnotations() 获取注解信息
Type[] getGenericInterface() 获取泛型信息

 

2. 代码示例

1 @Retention(RetentionPolicy.RUNTIME)
2 public @interface MyAnnotation {
3 }

 

 1 public class StudentTest {
 2 
 3     public static void main(String[] args) throws Exception {
 4 
 5         // 获取Student类型的Class对象
 6         Class c1 = Class.forName("com.lagou.task20.Student");
 7         System.out.println("获取到的包信息是:" + c1.getPackage());
 8         System.out.println("获取到的父类信息是:" + c1.getSuperclass());
 9 
10         System.out.println("-------------------------------------------------");
11         System.out.println("获取到的接口信息是:");
12         Class[] interfaces = c1.getInterfaces();
13         for (Class ct : interfaces) {
14             System.out.print(ct + " ");
15         }
16         System.out.println();
17 
18         System.out.println("-------------------------------------------------");
19         System.out.println("获取到的注解信息是:");
20         Annotation[] annotations = c1.getAnnotations();
21         for (Annotation at : annotations) {
22             System.out.print(at + " ");
23         }
24         System.out.println();
25 
26         System.out.println("-------------------------------------------------");
27         System.out.println("获取到的泛型信息是:");
28         Type[] genericInterfaces = c1.getGenericInterfaces();
29         for (Type tt : genericInterfaces) {
30             System.out.print(tt + " ");
31         }
32         System.out.println();
33     }
34 }

 

JavaSE:反射机制 - 获取其他结构信息

原文:https://www.cnblogs.com/JasperZhao/p/14889007.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!