1:通过反射解析这个类的构造函数
public class person { public String name="jack"; private int password=123; private static int age; public person() { System.out.println("person"); } public person(String name) { System.out.println("person name:"+name); } public person(String name,int age) { System.out.println("person name:"+name+"person age:"+age); } private person(List list) { System.out.println("person list"); } public void aa() { System.out.println("aa"); } public void aa1(String name,int age) { System.out.println("name:"+name+"age:"+age); } private void aa2(InputStream inputStream) { System.out.println("in"); } public static void aa3(int num) { System.out.println("aa3"+num); } }
2:反射这个类的构造函数
//反射类的构造函数,创建类的对象 /* * 反射類的構造方法的目的:創建類的對象,實例化對象 * 1:加載類class.forname * 2:獲取構造函數getConstructor * 3:運行構造方法(實例化)newInstance() */ public class Demo1 { // 反射构造函数 public person() @Test public void test1() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { // 先加载类,在获取类的构造函数(构造函数目的就是创建对象),在创建对象 Class c = Class.forName("com.reflction.person"); Constructor constructor = c.getConstructor(null);// 接受可变参数,null表示无参 person person = (com.reflction.person) constructor.newInstance(null); System.out.println(person.name); } // 反射构造函数 public person(String name) @Test public void test2() { try { Class c = Class.forName("com.reflction.person"); Constructor constructor1 = c.getConstructor(String.class); person person1 = (person) constructor1.newInstance("****"); System.out.println(person1.name); } catch (Exception e) { } } // 反射構造函數public person(String name,int age) @Test public void test3() { try { Class c = Class.forName("com.reflction.person"); Constructor constructor3 = c.getConstructor(String.class,int.class); person person3= (person) constructor3.newInstance("jack",23); System.out.println(person3.name); } catch (Exception e) { } } //反射構造函數 private person(List list) @Test public void test4() { try { Class c = Class.forName("com.reflction.person"); Constructor constructor4=c.getDeclaredConstructor(java.util.List.class); //私有的東西外界不能訪問,但反射可以,強制打開 constructor4.setAccessible(true);//暴力反射 ArrayList<String> list=new ArrayList<String>(); list.add("name"); list.add("age"); person person4=(person) constructor4.newInstance(list); System.out.println(person4.name); } catch (Exception e) { } } }
3:反射类的方法
//反射類的方法 /* * 反射類的方法的目的:運行該方法 * 1:加載類的對象Class.forname() * 2:獲取該方法getMethod,getDeclaredMethod() * 3:運行該方法invoke(); */ public class Demo2{ //反射類的public void aa() @Test public void test1() { try { person person=new person(); //加載類 Class c = Class.forName("com.reflction.person"); //獲取方法 Method method=c.getMethod("aa",null);//第一個參數那個方法,第二個參數 //讓方法運行 method.invoke(person, null);//讓那個對象運行 //比如:上廁所方法,肯定要誰去上廁所,要傳個person對象 } catch (Exception e) { } } //反射這個方法public void aa1(String name,int age) @Test public void test2() { try { person person=new person(); Class c = Class.forName("com.reflction.person"); Method method=c.getMethod("aa1",String.class,int.class); method.invoke(person,"jack",23); } catch (Exception e) { } } //反射這個方法private void aa2(InputStream inputStream) @Test public void test3() { try { person person=new person(); Class c = Class.forName("com.reflction.person"); Method method=c.getDeclaredMethod("aa2",InputStream.class);//private method.setAccessible(true);//暴力打開,沒有這句話的話private不能訪問 method.invoke(person, new FileInputStream("F:\1.txt")); } catch (Exception e) { } } //反射public static void aa3(int num) @Test public void test4()throws Exception { person person=new person(); Class c = Class.forName("com.reflction.person"); Method method=c.getMethod("aa3",int.class); method.invoke(null,23 );//static 方法不用傳入對象,當然也可以傳入 } }
3:反射类的字段
//反射字段 /* * 反射字段目的:獲取字段的值,或者為字段賦值封裝數據 * 1:加載類 * 2:獲取字段 * 3:取得那個字段的值 */ //public String n public class Demo3 { @Test public void test1()throws Exception { person person=new person(); //加載類 Class c = Class.forName("com.reflction.person"); //獲取字段 Field field=c.getField("name"); //獲取那個對象的字段,取字段的值 System.out.println(field.get(person)); //當然也可以設置字段的值 field.set(person, "xiaoli"); System.out.println(person.name); } //反射字段 private int password=123; @Test public void test2()throws Exception { person person=new person(); Class c = Class.forName("com.reflction.person"); Field field=c.getDeclaredField("password"); field.setAccessible(true);//暴力打開private System.out.println(field.get(person)); field.set(person, 456); System.out.println(field.get(person)); } }