Reflection
???? Under normal circumstances, we must know the complete path of a class before instantiating the object, but in Java, we can also find the information of the class where it is located through an object, this is actually the function of the class.
PackageReflect; ClassX { ???? } PublicClassGetclassdemo01 { ????PublicStaticVoidMain (string [] ARGs ){ ???????? X =NewX (); ???????? System.Out. Println (X. getclass (). getname ()); ????} } |
We can see that all the operations at this time are reversed, Which is reflection.
Class
???? The class itself represents the class itself. Through the class, you can obtain the complete structure of a class, including the method definition and attribute definition in this class.
?
No constructor is found in the JDK documentation, so such constructor is private.
Three methods for instantiating class objects
????? First: Use the forname () method
????? Type 2: pass class
????? Third: object. getclass ()
PackageReflect; ClassX { ???? } PublicClassGetclassdemo01 { ????PublicStaticVoidMain (string [] ARGs ){ ???????? Class <?> C1 =Null; ???????? Class <?> C2 =Null; ???????? Class <?> C3 =Null; ????????Try{ ???????????? // Instantiate the class object in the first way. This method is also the most commonly used form. ???????????? C1 = Class.Forname("Reflect. X "); ???????????? // Instantiate the class object through the disallow in the object class ???????????? C2 =NewX (). getclass (); ???????????? // Instantiate a class through class. Cass ???????????? C3 = x.Class; ???????????? System.Out. Println ("Class Name:" + c1.getname ()); ???????????? System.Out. Println ("Class Name:" + c2.getname ()); ???????????? System.Out. Println ("Class Name:" + c3.getname ()); ????????}Catch(Classnotfoundexception e ){ ???????????? //TodoAuto-generated Catch Block ???????????? E. printstacktrace (); ????????} ????} } |
?
?
?
?
?
?
?
?
?
?
?
?
?
?
Black Horse programmer _ reflection