Java-reflect Special Topics
1. Use of Class
2. Reflection of the method
3. Reflection of member variables
4. Reflection of the constructor function
5. Java class loading mechanism
Class
1. In an object-oriented world, everything is object.
In the Java language, static members, ordinary data type classes are not objects?
Whose object is the class?
Class is an object, and the class is an instance object of the Java.lang.Class class (there is a class named Class)
Public classMain { Public Static voidMain (string[] args) {//how the instance object of Foo representsFoo fool =NewFoo (); //Foo This class is also an instance object, the Class class instance object, how to express it//Any class is an instance object of class, and this instance object is represented in three ways//the first representation---> actually tells us that any class has an implied static member variable classClass C1 = Foo.class; //the second representation is already known to the object of the class through the GetClass methodClass C2 =Fool.getclass (); /** Official website C1,C2 indicates the class type of the Foo class (class type) * Everything is object, * class is also an object, is an instance object of class classes * This object we call the class type of the class */ //whether C1 or C2 represent the class type of the Foo class, a class can only be an instance object of classSYSTEM.OUT.PRINTLN (C1 = = C2);//true//The Third Way of expressionClass C3 =NULL; Try{C3= Class.forName ("Foo");//Classthe full path name}Catch(ClassNotFoundException e) {e.printstacktrace (); } System.out.println (C3= = C2);//true//We can create an object instance of the class with the class type of the class completely. Try{foo Foo=(Foo) c1.newinstance (); Foo.print ();//Foo}Catch(instantiationexception e) {e.printstacktrace (); } Catch(illegalaccessexception e) {e.printstacktrace (); } }}classfoo{voidprint () {System.out.println ("Foo"); }}
2. Class.forName ("Full name of the class")
Not only represents the class type of the class, it also represents the dynamic load class
Please distinguish between compiling, running
Compile time load class is static to load class, run time load class is dynamic load class
3. class type corresponding to the basic data type
Public classClassDemo2 { Public Static voidMain (string[] args) {Class C1=int.class;//class type of intClass C2 = String.class;//String class type string byte-codeClass C3 =Double.class; Class C4= Double.class; Class c5=void.class; System.out.println (C1.getname ());//intSystem.out.println (C2.getname ());//java.lang.String//the name of the class that does not contain the package nameSystem.out.println (C2.getsimplename ());//StringSystem.out.println (C3.getname ());//DoubleSystem.out.println (C4.getname ());//java.lang.DoubleSystem.out.println (C5.getname ());//void }}
4. Basic API operations for class
Get information about all methods of a class
Public class Classdemo { publicstaticvoid main (string[] args) { = "Hello" ; Classutil.printclassmessage (s); }}
Public classClassutil {/*** Print the class information, including the class's member functions, member variables *@paramObject the information of the class to which the objects belong*/ Public Static voidPrintclassmessage (Object object) {//to get information about a class, first get the class type of the classClass C = Object.getclass ();//The object C of which child class is passed is the class type of the subclass//gets the name of the classSystem.out.println ("The name of the class is:" +c.getname ()); /**method class, Method Object * A member method is a Methods object * the GetMethods () method gets all of the public functions, including inherited by the parent class * GETDE Claredmethods () Gets all the methods that the class declares itself, regardless of access rights*/method[] Ms= C.getmethods ();//c.getdeclaredmethods (); for(inti=0; i<ms.length; i++){ //gets the class type of the return value type of the methodClass ReturnType =Ms[i].getreturntype (); System.out.print (Returntype.getname ()+" "); //get the name of the methodSystem.out.print (Ms[i].getname () + "("); //gets the parameter type---> Gets the class type of the type of the parameter listclass[] Paramtypes =ms[i].getparametertypes (); for(Class class1:paramtypes) {System.out.print (Class1.getname ()); } System.out.println (")"); } }}
Get information about a member variable
Java Advanced-reflection mechanism