码迷,mamicode.com
首页 > 编程语言 > 详细

反射-基础方法-java

时间:2019-09-29 22:10:23      阅读:82      评论:0      收藏:0      [点我收藏+]

标签:ons   const   判断   catch   col   final   isp   div   style   

 1 import net.dsmxx.PersonFk;
 2 import org.apache.poi.ss.formula.functions.T;
 3 
 4 import java.lang.annotation.Annotation;
 5 import java.lang.reflect.Constructor;
 6 import java.lang.reflect.Field;
 7 import java.lang.reflect.Method;
 8 import java.lang.reflect.Modifier;
 9 
10 /**
11  * Created with IDEA
12  * author:foreign
13  * Date:2019/9/29
14  * Time:20:02
15  */
16 public class ReflectionFk {
17     public static void main(String[] args) {
18         //1. 编译期获取对象
19         Class clazz = PersonFk.class;
20         System.out.println("对象:" + clazz);
21         try {
22             //1.1 运行期获取对象
23             Class clazz1 = Class.forName("net.dsmxx.PersonFk");
24             System.out.println("对象:" + clazz1);
25         } catch (ClassNotFoundException e) {
26             e.printStackTrace();
27         }
28         //2. 获取全类名(包含包名)
29         String name = clazz.getName();
30         System.out.println("全类名:" + name);
31         //2.1 获取类型
32         String simpleName = clazz.getSimpleName();
33         System.out.println("类名:" + simpleName);
34         //3 获取类的修饰符
35         int modifiers = clazz.getModifiers();
36         System.out.println("修饰符:" + modifiers);
37         //3.1 判断此类是public、final、native、state、synchronized、transient
38         boolean aPublic = Modifier.isPublic(modifiers);
39         System.out.println("是否public:" + aPublic);
40         boolean aFinal = Modifier.isFinal(modifiers);
41         System.out.println("是否final:" + aFinal);
42         boolean aNative = Modifier.isNative(modifiers);
43         System.out.println("是否aNative: " + aNative);
44         boolean aStatic = Modifier.isStatic(modifiers);
45         System.out.println("是否static:" + aStatic);
46         boolean aSynchronized = Modifier.isSynchronized(modifiers);
47         System.out.println("是否synchronized:" + aSynchronized);
48         boolean aTransient = Modifier.isTransient(modifiers);
49         System.out.println("是否transient:" + aTransient);
50         //3.2 判断此类中的方法是什么修饰
51         Method[] methods = clazz.getMethods();
52         for (Method method : methods) {
53             int modifiers1 = method.getModifiers();
54             boolean aPublic1 = Modifier.isPrivate(modifiers1);
55             System.out.println("方法是否private:" + aPublic1);
56         }
57         //4 获取包信息
58         Package aPackage = clazz.getPackage();
59         System.out.println("包名:" + aPackage);
60         //5 获取父类
61         Class superclass = clazz.getSuperclass();
62         System.out.println("父类:" + superclass);
63         //6 获取当前类实现的接口,不包括父类实现的接口
64         Class[] interfaces = clazz.getInterfaces();
65         for (Class inter : interfaces) {
66             System.out.println("接口:" + inter);
67         }
68         //7 获取构造器
69         Constructor[] constructors = clazz.getConstructors();
70         for (Constructor con : constructors) {
71             System.out.println("构造器:" + con);
72         }
73         //8 获取方法
74         Method[] methods1 = clazz.getMethods();
75         for (Method method : methods1) {
76             System.out.println("方法:" + method);
77         }
78         //9 获取public变量
79         Field[] fields = clazz.getFields();
80         for (Field field : fields) {
81             System.out.println("变量:" + field);
82         }
83         //10 获取注解
84         Annotation[] annotations = clazz.getAnnotations();
85         for (Annotation annotation : annotations) {
86             System.out.println("注解:" + annotation);
87         }
88     }
89 }

 

反射-基础方法-java

标签:ons   const   判断   catch   col   final   isp   div   style   

原文地址:https://www.cnblogs.com/fangke/p/11609904.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!