標籤:
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()@Testpublic 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)@Testpublic 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)@Testpublic 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)@Testpublic 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 npublic class Demo3 {@Testpublic 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;@Testpublic void test2()throws Exception{person person=new person();Class c = Class.forName("com.reflction.person");Field field=c.getDeclaredField("password");field.setAccessible(true);//暴力打開privateSystem.out.println(field.get(person));field.set(person, 456);System.out.println(field.get(person));}}
java反射經典總結