Ruchunli's work notes , who says programmers can't have a literary fan?
Java programs can dynamically load, parse, and use some types that are not deterministic during the compilation phase, known as reflection (Reflection) or introspection (introspection). Java programs can load a runtime to know the name of a class, learn its full construct (but not including the methods definition), and generate its object entities, or set values on its fields, or evoke its methods. This ability to "See Through Class" (the ability of the program Toexamine itself) is called introspection (introspection, vipassana, introspection). Reflection and introspection are the two terms that are often and are mentioned.
Class is the core class in the reflection API. When the class loads, the Java virtual opportunity automatically creates the corresponding class object.
The GetClass () method is defined in the Java.lang.Object class, so for any Java object, the class of the object can be obtained by this method.
Instances of class classes are used to represent runtime Java data types, including classes, interfaces, arrays, enumerations, annotations, basic data types, and even void.
Use the Reflection steps to summarize:
First, get the Java.lang.Class object of the class you want to manipulate
Get the Class object:
Call static method: Class.forName ();
Class.forName ("P1. Person "); Class.forName ("Com.mysql.jdbc.Driver");
Call the GetClass () method defined in the object class
person person = new person (); Class cs = P.getclass (); Class strclass = "Hello World". GetClass ()
Using the. Class expression
Class strclass = String.class; Class Personclass = p1. Person.class; Class jdbcclass = Com.mysql.jdbc.Driver.class;
Using the. Class expression
Class intclass = Int.class; Class dobclass = Double.class; Class voidclass = Void.class;
Call the Type property of the corresponding wrapper class
Class integerclass = Integer.type; Class voicclass = Void.type;
Ii. Obtaining member information for a class
Invoke methods such as Getdeclaredmethods
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/83/90/wKiom1d1_3qzmK6GAAQgDJUsMjA120.jpg "title=" Qq20160701132742.jpg "alt=" Wkiom1d1_3qzmk6gaaqgdjusmja120.jpg "/>
iii. using the Reflection API to manipulate member information for a class
In the JDK, the Java reflection mechanism is implemented primarily by the following classes, which are located in the Java.lang.reflect package.
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/83/90/wKiom1d2ABChBtthAAd1S-Qwee0215.jpg "title=" Qq20160701133030.jpg "alt=" Wkiom1d2abchbtthaad1s-qwee0215.jpg "/>
This article is from the "world of Stuffy Gourd" blog, please be sure to keep this source http://luchunli.blog.51cto.com/2368057/1794856
Reflection of Java Learning Notes