标签:
Java反射机制使我们在写代码时可以动态的初始化我们要使用的类,灵活的使用java资源,好处太多了,网上搜搜一大堆,我就闲话少说直接上代码吧。
Class c = Class.forName("全路径", false, Thread.currentThread().getContextClassLoader()); Package pk = c.getPackage(); Class cParent = c.getSuperclass(); ClassLoader cLoader = c.getClassLoader(); ClassLoader pLoader = cLoader.getParent(); //获得修饰符比如private public protected等,class field method都有Modifers int m = c.getModifiers(); System.out.print(Modifier.toString(m) + " "); if (Modifier.isInterface(m)) { System.out.print("interface "); } else { System.out.print("class "); }可以获得程序包package这个作用太大了,尤其是android开发好多信息在package里呢,也可以获得classloader一起父类和父ClassLoader等,下面我就不说了,大家直接看注释吧,直接上代码:
//属性==================== Field[] fields = c.getDeclaredFields();//获取所有包括private的 Field testInt = c.getField("testInt");//根据属性名字直接得到想要的属性 for (Field field : fields) { if (Integer.class.isAssignableFrom(field.getType())) { field.setAccessible(true); field.setInt(c.newInstance(), 2); } System.out.print("\t" + Modifier.toString(field.getModifiers())); // field是什么type System.out.print(" " + field.getType().getName() + " "); // field的name System.out.println(field.getName() + ";"); } // 构造器=================== Constructor[] constructors = c.getDeclaredConstructors();//获得所有的构造器, // getDeclaredConstructor(Class<?>... parameterTypes)获取指定参数class类型的那个构造器 for (Constructor constructor : constructors) { System.out.print("\t" + Modifier.toString( constructor.getModifiers())); System.out.println(" " + constructor.getName() + "();"); } //得到一个有两个参数的构造器,并且产生一个实例 Class[] params = new Class[2]; params[0] = String.class; params[1] = Integer.TYPE; Constructor constructor = c.getConstructor(params); Object[] argObjs = new Object[2]; argObjs[0] = "caterpillar"; argObjs[1] = new Integer(90); Object obj = constructor.newInstance(argObjs); System.out.println(obj); //方法================= Method[] methods = c.getDeclaredMethods(); for (Method method : methods) { System.out.print("\t" + Modifier.toString( method.getModifiers())); System.out.print(" " + method.getReturnType().getName() + " "); System.out.println(method.getName() + "();"); } //调用某个反射得到的方法 Object targetObj = c.newInstance(); Class[] param1 = {String.class}; Method setNameMethod = c.getMethod("setName", param1);//传入指定方法名字和参数的class类型 Object[] argObjs1 = {"caterpillar"}; if (Modifier.isPrivate(setNameMethod.getModifiers())) { setNameMethod.setAccessible(true); } setNameMethod.invoke(targetObj, argObjs1); //数组 反射的使用============== Object objArr = Array.newInstance(c, 5);//一个是数组元素class类型,一个是长度 for(int i = 0; i < 5; i++) { Array.set(objArr, i, i+""); } for(int i = 0; i < 5; i++) { System.out.print(Array.get(objArr, i) + " "); } System.out.println(); String[] strs = (String[]) objArr; for(String s : strs) { System.out.print(s + " "); }
标签:
原文地址:http://blog.csdn.net/watertekhqx/article/details/51361680