java中反射是通過一定的條件得到類對(duì)象然后進(jìn)行各種操作的過程;
反射有可以分為幾個(gè)層次弧满;
1 得到類對(duì)象
2. 得到構(gòu)造方法
3. 得到屬性
4. 得到方法
## 反射(重要)
? ? 重要的關(guān)鍵字的關(guān)鍵字
? ? ? ? Class? ? ? ? ? ? ? //獲得類對(duì)象,大前提
? ? ? ? ? ? Constructor? ? //獲得類中的構(gòu)造方法阀坏,二前提
? ? ? ? ? ? ? ? Method? ? //獲得類中的方法
? ? ? ? ? ? ? ? ? ? invoke? //對(duì)獲得到的方法傳參
? ? ? ? ? ? ? ? Filed? ? ? //獲得類中的變量
? ? ? ? ? ? ? ? ? ? set? ? //對(duì)獲得到的變量進(jìn)行設(shè)置
? ? 1. 獲得類對(duì)象? (關(guān)鍵字Class)?
? ? ? ? 三種方法
? ? ? ? 1)通過包名獲取
? ? ? ? ? ? Class cls1 = Class.forName("com.reflect.Person");? //括號(hào)里面是包名蹋订,具體到類橄教,Person是一個(gè)類名
? ? ? ? 2)通過類名獲取
? ? ? ? ? ? Class cls2 = Person.Class;
? ? ? ? 3) 通過一個(gè)構(gòu)造方法獲取
? ? ? ? ? ? Class cls3 = new Person(1, "小美").getClass();? ? ? //Person(1, "小美")? 是Person類的一個(gè)有參的構(gòu)造方法
? ? 2. 獲得構(gòu)造方法(關(guān)鍵字 Constructor)
? ? ? ? 先找到類對(duì)象:
? ? ? ? ? ? Class cls = Class.forName("com.reflect.Person");
? ? ? ? //獲得多個(gè)構(gòu)造方法
? ? ? ? 1)? 獲取所有非私有化的構(gòu)造方法
? ? ? ? ? ? Constructor[] constructors = cls.getConstructors();
? ? ? ? 2) 獲得所有構(gòu)造方法(包括私有和非私有)
? ? ? ? ? ? Constructor allConstructs = cls.getDeclaredConstructors();
? ? ? ? //獲得指定構(gòu)造方法,非私有
? ? ? ? 3) Constructor aConstructor = cls.getConstructor(int.class, String,class);? //括號(hào)里邊是獲得構(gòu)造方法需傳入的參數(shù)類型
? ? ? ? ? ? (1)利用獲得到的構(gòu)造方法創(chuàng)造一個(gè)新的對(duì)象
? ? ? ? ? ? ? ? Person p = (Person) aConstructor.newInstance(1, "妹子");
? ? ? ? ? ? ? ? ? ? p.sleep();
? ? ? ? ? ? ? ? ? ? p.game();
? ? ? ? 獲得私有化的構(gòu)造方法
? ? ? ? 4) Constructor privateConstructor = cls.getDeclaredConstructor(null);? ? //這是一個(gè)無參的構(gòu)造方法
? ? ? ? ? ? (1) 用得到的構(gòu)造方法創(chuàng)造對(duì)象浩村,需要授權(quán)
? ? ? ? ? ? ? ? privateConstructor.setAccessible(true);
? ? ? ? ? ? ? ? Person p2 = (Person) privateConstructor.newInstance(null);
? ? ? ? ? ? ? ? p2.setId(3);
? ? ? ? ? ? ? ? p2.setName("小媚");
? ? 3. 獲得成員方法(關(guān)鍵字 Method)
? ? ? ? 首先創(chuàng)建類對(duì)象
? ? ? ? ? ? Class cls = Class.forName("com.reflect.Person");
? ? ? ? 獲得所有方法:
? ? ? ? 1) 獲得所有方法(非私有,同時(shí)會(huì)獲取到父類的方法)
? ? ? ? ? ? Method[] allMethod = cls.getMethods();
? ? ? ? 2) 暴力方式獲得所有方法(包括私有和非私有做葵,會(huì)過濾掉父類的方法)
? ? ? ? ? ? Method[] allMethods = cls.getDeclareMethods();
? ? ? ? 獲得指定方法:
? ? ? ? ? ? Person aPerson = (Person) cls.getConstructor(int.class, String.class).newInstance(1, "小花");
? ? ? ? 3) 獲取一個(gè)指定的普通公共方法:
? ? ? ? ? ? Method aMethod = cls.getMethod("test", int.class);? ? ? ? ? //? "test" 是指定方法的方法名, int.class 是使用方法需傳入?yún)?shù)的類型
? ? ? ? ? ? aMethod.invoke(aPerson, 6);? ? ? ? ? ? ? ? //對(duì)參數(shù)進(jìn)行設(shè)置
? ? ? ? 4) 獲取一個(gè)靜態(tài)的方法
? ? ? ? ? ? Method aStaticMethod = cls.getMethod("ear", null);? ? ? ? ? //? null代表此方法不需要傳入任何參數(shù)
? ? ? ? ? ? aStaticMethod.invoke(null, null);? ? ? ? ? ? ? ? ? ? ? // 因?yàn)闆]有任何參數(shù)心墅,所有不需要設(shè)置值
? ? ? ? 5) 用暴力方法獲取一個(gè)私有化方法
? ? ? ? ? ? Method aPrivateMethod = cls.getDeclaredMethod("testPrivate", null);? ? ? ? //私有化不需要傳入?yún)?shù)所以是null
? ? ? ? ? ? aPrivateMethod.getAccessible(true);? ? ? ? ? ? ? ? ? ? //給方法授權(quán)
? ? ? ? ? ? aPrivateMethod.invoke(aPerson, null);? ? ? ? ? ? ? //給私有化設(shè)置值
? ? 4. 獲得成員變量
? ? ? ? 首先創(chuàng)建類對(duì)象
? ? ? ? ? ? Class cls = Class.forName("com.reflect.Person");
? ? ? ? 獲得所有變量:
? ? ? ? 1) 獲得所有變量(非私有)
? ? ? ? ? ? Field[] aField = cls.getFields();
? ? ? ? 2) 暴力方法酿矢,獲得所有變量(包括私有和非私有)
? ? ? ? ? ? File[] aFileds = cls.getDeclaredFields();
? ? ? ? 3) 獲得指定變量(非私有)
? ? ? ? ? ? Field aPublicField = cls.getField("test");? ? ? ? ? //括號(hào)內(nèi)是指定變量的變量名
? ? ? ? ? ? aPublicField.set(aPerson, 5);? ? ? ? ? ? ? ? ? //對(duì)獲得到的變量賦值
? ? ? ? ? ? System.out.println(aPerson.test);? ? ? ? ? ? ? //變量的使用方法
? ? ? ? 4) 暴力方法,獲得私有化變量
? ? ? ? ? ? Field aPrivateField = cls.getDeclaredfField("id");? ? ?
? ? ? ? ? ? aPrivateField.setAccessible(true);? ? ? ? ? //對(duì)變量重新賦值之前需要對(duì)變量授權(quán)
? ? ? ? ? ? aPrivateField.set(aPerson, 8);? ? ? ? ? ? ? //對(duì)變量賦值