First, the reflection call the specified method
By reflection, calling methods in the class, is completed by Method class. step:
getMethod (String name, Class ... parameterTypes) 1. achieved by a method of the class Class Method object, and set the parameters of this type of method of operation required.
2. After using the Object invoke (Object obj, Object [] args) call, passing the object obj parameter information to be set method.
Package day19; Import java.lang.reflect.Constructor; Import as java.lang.reflect.Field; Import the java.lang.reflect.Method; public class Test1 { public static void main (String [] args) { the try { Class clazz = Class.forName ( "day19.Student" ); / ** * NOTE: the following reflection is whether to call setInfo method, or the test method, the object obj method is invoked, the object is actually a student obj object * * / Constructor CON clazz.getConstructor = (); // get no argument constructor Object obj = con.newInstance (); // create an object using a constructor with no arguments // If the method you want to call a public Method, m = clazz.getMethod ( "setInfo", String. Class , String. Class ); // get name called setInfo, the parameter is String, String method of m.invoke (obj, "Joe Smith", "first school"); // parameter 1 is a need to instantiate an object, calling the parameter 2 is the actual parameters of the current method // method if you want to call a private method m1 = clazz.getDeclaredMethod ( . "Test", String class ); // Get Test method called, a parameter of type String method m1.setAccessible ( to true ); // release private package, the following method can be forced to call the private m1.invoke (obj, "John Doe" ); // call to an overloaded method method m2 = clazz.getMethod ( "setInfo" ,int.class ); // overloads setInfo of m2.invoke (obj, 10); // Parameter 1 is a need to instantiate an object, calling the parameter 2 of the method of the current actual parameters // method call returns a value Method m3 clazz.getMethod = ( "getSchool"); // this is a method for obtaining a method called getSchool and no parameters String School = (String) m3.invoke (obj); // call returns a value, but no parameters method System .out.println (School); } the catch (Exception E) { // the TODO Auto-Generated Block the catch e.printStackTrace (); } } }
Print Results:
Second, call the specified property reflection mechanism
1. In the reflection, the direct operation by Filed class class attributes, SET () and get () method provided by the Field class can obtain setup and operation attribute of the content.
2.public Field getField (String name) Returns the Class object represented by the specified public class or interface Field.
Specified Field 3.public Field getDeclaredField (String name) Returns the Class object represented by the class or interface.
4. In the Field:
①public Object get (Object obj) to obtain the contents of this Field property of the specified object obj
②public void set (Object obj.Object value) of this set attributes of a specified object obj Field
NOTE: In the class properties are set under the premise private, when using the set () and get () method, first using the Field class setAcessible (true) method property to be externally accessible to be operated.
③public void setAccessible (true) to access private property, so the property is visible.
Package day19; Import java.lang.reflect.Constructor; Import as java.lang.reflect.Field; Import the java.lang.reflect.Method; public class Test1 { public static void main (String [] args) { the try { Class clazz = the Class.forName ( "day19.Student" ); // reflection to create an object the Constructor CON = clazz.getConstructor (); Student STU = (Student) con.newInstance (); Field, F = clazz.getField ( "School"); // get the name of school property f.set (stu, "Third Middle"); // school property settings "third school" stu object of the String school = (String) f.get (stu); // Get stu object properties school value System.out.println (School); // if it is a private property Field, F1 = clazz.getDeclaredField ( "privateField" ); f1.setAccessible ( to true ); // release force can call the private property package below f1. set (stu, "private property test" ); System.out.println (f1.get (STU)); } the catch (Exception E) { // the TODO Auto-Generated Block the catch e.printStackTrace (); } } }
Print Results: