Java 反射 调用 demo
基础类.供demo反射练习
1 package fanshe; 2 3 public class Person { 4 public String name; 5 private String age; 6 private int p; 7 public Person() {}; 8 public Person(String name, String age) { 9 super(); 10 this.name = name; 11 this.age = age; 12 } 13 private Person(String name, String age,int p) { 14 super(); 15 this.name = name; 16 this.age = age; 17 this.p = p; 18 } 19 public String getName() { 20 return name; 21 } 22 @Override 23 public String toString() { 24 return "Person [name=" + name + ", age=" + age + "]" + "p -"+ p; 25 } 26 public void setName(String name) { 27 this.name = name; 28 } 29 public String getAge() { 30 return age; 31 } 32 public void setAge(String age) { 33 this.age = age; 34 } 35 }
反射demo类
1 package fanshe; 2 3 import java.lang.reflect.Constructor; 4 import java.lang.reflect.Field; 5 import java.lang.reflect.InvocationTargetException; 6 import java.lang.reflect.Method; 7 8 public class demo1 { 9 10 public static void main(String[] args) { 11 // TODO Auto-generated method stub 12 try { 13 d_3(); 14 } catch (Exception e) { 15 // TODO: handle exception 16 e.printStackTrace(); 17 } 18 } 19 20 private static void d_1() { 21 Person p = new Person(); 22 Class c = p.getClass(); 23 24 } 25 26 private static void d_2() { 27 Class c = Person.class; 28 System.out.println(c); 29 } 30 private static void d_3() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchFieldException { 31 Class c = Class.forName("fanshe.Person"); 32 33 //--------------------------// 34 Constructor con = c.getConstructor(); //获得 空参数的构造器 public 35 Object o= con.newInstance(); //运行获取到的构造方法 36 System.out.println(o.toString()); 37 38 //--------------------------// 39 Constructor con2 = c.getConstructor(String.class,String.class); //获得带有参数的构造方法 public 40 Object o2 = (Person)con2.newInstance("a","22"); 41 System.out.println(o2.toString()); 42 43 //--------------------------// 44 45 Constructor con3 = c.getDeclaredConstructor(String.class,String.class,int.class); //获得带有参数的构造方法 private 46 con3.setAccessible(true); 47 Object o3 = (Person)con3.newInstance("a","22",14); 48 System.out.println(o3.toString()); 49 50 //--------------------------// 51 Object tmp_obj = c.newInstance(); 52 Field f_name = c.getField("name");//获得成员变量 public 53 f_name.set(tmp_obj, "abc"); //设置变量 54 System.out.println(tmp_obj); 55 56 //--------------------------// 57 Field f_age = c.getDeclaredField("age");//获得成员变量 public 58 System.out.println(f_age); 59 60 //--------------------------// 61 Method m_getName = c.getMethod("getName"); //获得类函数无参数 public 62 Object o1 = m_getName.invoke(tmp_obj); //运行getName方法.返回值 反给obj 63 System.out.println(o1); 64 65 //--------------------------// 66 Method m_setName = c.getMethod("setName",String.class); 67 m_setName.invoke(tmp_obj,"abcdqweqwe"); 68 System.out.println(tmp_obj.toString()); 69 } 70 }
通过反射.绕开泛型
1 ArrayList<String> arr = new ArrayList<String>(); 2 arr.add("a"); 3 Class carr = arr.getClass(); 4 Method arr_add = carr.getMethod("add",Object.class); 5 arr_add.invoke(arr, 1); 6 arr_add.invoke(arr, 2); 7 arr_add.invoke(arr, 3); 8 arr_add.invoke(arr, 4); 9 System.out.println(arr);