/** Make a little progress every day
*/
// The package reads the persontype = com. anby. damain. Person in the properties file.
Package COM. anby. demo; import Java. lang. reflect. field; import Java. util. hashmap; import Java. util. map; import Java. util. resourcebundle; public class reflect {public static void main (string [] ARGs) throws exception {// set the dataset Map <string, string> datamap = new hashmap <string, string> (); datamap. put ("name", "anby"); datamap. put ("Age", "20"); datamap. put ("sex", "man"); datamap. put ("QQ", "190241347"); datamap. put ("EMA Il "," ailaohuyou123@yahoo.com "); Class clzz = Class. forname (getproperties ("persontype"); // if an exception is reported, the file access permission is insufficient (Java. lang. illegalaccessexception) // retrieves the Instance Object OBJ = clzz. newinstance (); // obtain the field [] field = clzz in all objects. getdeclaredfields (); For (field F: Field) {f. setaccessible (true); string fieldname = f. getname (); string value = datamap. get (fieldname); If (value! = NULL) {f. set (OBJ, value) ;}} system. out. println (OBJ);} public static string getproperties (string key) {// * obtain the field // 1 under properties and specify the resource file name resourcebundle RB = resourcebundle. getbundle ("data"); // 2. Obtain the value of the corresponding field in the resource file string type = RB. getstring (key); return type ;}}
Package COM. anby; import Java. lang. reflect. constructor; import Java. lang. reflect. field; import Java. lang. reflect. invocationtargetexception; import Java. lang. reflect. method; // a brief description of the reflection public class clssdemo {public static void main (string [] ARGs) throws exception {class CLS = Class. forname ("com. anby. student "); object OBJ = Cls. newinstance (); method [] M = Cls. getdeclaredmethods (); For (INT I = 0; I <m. length; I ++) {system. out. println (M [I]. getname (); // The method cannot print the result. Only the method M1 = CLS can be used. getdeclaredmethod ("print", null ); // Method M1 = Cls. getdeclaredmethod ("print", String. class, Int. class); m1.invoke (); // call this method} public static void getconstructor () throws exception {string Stu = "com. anby. student "; // get the class name through the bytecode and place it in the memory class clss = Class. forname (Stu); // gets the default constructor cons1 = clss. getconstructor (null); system. out. println (cons1); // get the instance of this class \ // two call methods constructed by default // 1, get object obj1 = cons1.newinstance (null) through constructor object of constructor ); // 2. Obtain object obj2 = clss through the class object. newinstance (); // get the constructor cons2 = clss of the specified parameter (including private. getdeclaredconstructor (string. class, integer. class); // set the access permission cons2.setaccessible (true); // use a private constructor to initialize the object obj3 = cons2.newinstance ("ASD", 123);} public static void getfield () throws exception {// Similarly, fields, methods, and enumeration can all be obtained. // field [] field = clss. getdeclaredfields (); // system. out. println (field); Class C = Class. forname ("com. anby. student "); object obj4 = C. newinstance (); field namef = C. getdeclaredfield ("name"); field agef = C. getdeclaredfield ("Age"); namef. setaccessible (true); // when accessing a private field, if this sentence is not added, Java is thrown. lang. illegalaccessexceptionnamef. set (obj4, "James"); object namevalue = namef. get (obj4); agef. get (obj4); agef. set (obj4, 123); object agevalue = agef. get (obj4); system. out. println (namevalue); system. out. println (agevalue); // set the age value} class student {private string name = "anby"; Public int age = 18; Public student () {system. out. println ("default constructor");} private student (string name, integer age) {system. out. println ("constructor with Parameters");} public void print () {system. out. println ("print ");}} |