反射之基本方法,这块代码请从下往上看,为了调试,本人都注释了,自己搞一下~)
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException, NoSuchFieldException { /** * 反射机制的动态代理 */ //这边再说 /** * 通过反射机制操作某个类的属性 */ // Class class1 = Class.forName("ModelC"); // Object obj = class1.newInstance(); // // 可以直接对 private 的属性赋值 // Field field = class1.getDeclaredField("name"); // //设置些属性是可以访问的 // field.setAccessible(true); // field.set(obj, "老王"); // System.out.println(field.get(obj)); /** * 通过反射机制调用某个类的方法 */ // Class class1 = Class.forName("ModelC"); // Method method3 = class1.getMethod("say"); // method3.invoke(class1.newInstance()); // // Method method = class1.getMethod("daYin", String.class); // method.invoke(class1.newInstance(), "hahahaha"); // // Method method2 = class1.getMethod("jiafa", int.class, int.class); // method2.invoke(class1.newInstance(), 1,2); /** * 获得类的全部方法 */ // Class class1 = Class.forName("ModelC"); // Method[] methods = class1.getMethods(); // for(int i = 0; ireturnType = methods[i].getReturnType(); // Class para[] = methods[i].getParameterTypes(); // int temp = methods[i].getModifiers(); // System.out.print(Modifier.toString(temp) + " "); // System.out.print(returnType.getName() + " "); // System.out.print(methods[i].getName() + " "); // System.out.print("("); // for (int j = 0; j < para.length; ++j) { // System.out.print(para[j].getName() + " " + "arg" + j); // if (j < para.length - 1) { // System.out.print(","); // } // } // Class exce[] = methods[i].getExceptionTypes(); // if (exce.length > 0) { // System.out.print(") throws "); // for (int k = 0; k < exce.length; ++k) { // System.out.print(exce[k].getName() + " "); // if (k < exce.length - 1) { // System.out.print(","); // } // } // } else { // System.out.print(")"); // } // System.out.println(); // } /** * 获得类的全部属性 */ // Class class1 = Class.forName("ModelC"); // // Field[] fields = class1.getDeclaredFields(); // for(int i=0;i type = fields[i].getType(); // System.out.println(priv+" "+type.getName()+" "+fields[i].getName()); // } /** * 获得父类,或者接口的属性 */ // Class class1 = Class.forName("ModelC"); // Field[] fields = class1.getFields(); // for(int i=0;i type = fields[i].getType(); // System.out.println(priv+" "+type.getName()+" "+fields[i].getName()); // } /** * 获得类构造方法,通过构造方法建对象 */ // Class class1 = Class.forName("ModelC"); // System.out.println(class1.getName()); // ModelC modelC = (ModelC)class1.newInstance(); // modelC.setName("fw"); // modelC.setAge(13); // System.out.println(modelC.toString()); // // Constructor constructors[] = class1.getConstructors(); // ModelC modelC2 = (ModelC)constructors[1].newInstance("fw2"); // System.out.println("modelC2:"+modelC2.toString()); // for(int i=0; i clazzs[] = constructors[i].getParameterTypes(); // for (int j = 0; j < clazzs.length; j++) { // if (j == clazzs.length - 1) // System.out.print(clazzs[j].getName()); // else // System.out.print(clazzs[j].getName() + ","); // } // System.out.println(")"); // } /** * 获得接口 */ // Class class1 = Class.forName("Adapter.PlayGanmes"); // Class intf[] = class1.getInterfaces(); // for(int i = 0 ; i class1 = Class.forName("Bridge.Black"); // Class class2 = class1.getSuperclass(); // System.out.print(class2.getName()); }
引用到的两个类:
public class ModelC extends Father{ private String name; private int age; public ModelC(){ super(); } public ModelC(String name){ this.name = name; } public ModelC(String name,int age){ this.name = name; this.age = age; } public void say(){ System.out.println("hello"); } public void daYin(String oo){ System.out.println(oo); } public void jiafa(int a1,int a2){ System.out.println(a1+a2); } private void method1(){ System.out.println(this.name+","+this.age); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String toString(){ return "[name:"+this.name+", age:"+age+"]"; } }
public class Father { public String sex; public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }