1 Packagecn.itcast_02;2 3 ImportJava.lang.reflect.Constructor;4 5 /*6 * Requirements: Get the construction method by reflection and use:7 * Public Person (string name, int age, string address)8 * 9 * Person p = new person ("Brigitte", 27, "Beijing");Ten * SYSTEM.OUT.PRINTLN (p); One */ A Public classReflectDemo2 { - Public Static voidMain (string[] args)throwsException { - //Get bytecode File object theClass C = class.forname ("Cn.itcast_01.Person"); - - //Get the parameter construction method object - //Public constructor<t> GetConstructor (class<?> ... parametertypes) +Constructor con = c.getconstructor (String.class,int.class, -String.class); + A //To create an object by using the parameter construction method object at //Public T newinstance (Object ... initargs) -Object obj = con.newinstance ("Brigitte", 27, "Beijing"); - - System.out.println (obj); - } -}
Android (Java) Learning Note 107: Get the parameter construction method by reflection and use