Java Reflection Learning
The so-called reflection can be understood as the operation of obtaining the object type information during the run time. Traditional programming methods require programmers to decide which type to use during the compilation phase, but with the help of reflection, programmers can dynamically obtain this information to write more portable code. Strictly speaking, reflection is not a feature of programming languages, because the reflection mechanism can be implemented in any language, but the implementation of reflection is much more convenient if the programming language itself supports reflection.
1, Get type class
We know that in Java everything is an object, and we generally use objects that are directly or indirectly inherited from the object class. The object class contains a method called GetClass, which allows you to obtain an instance of the type class. A type class refers to a class that represents a type, because everything is an object, and the type is no exception, and in Java the Type class is used to represent a type. All type classes are instances of class classes. For example, there is the following code:
A = new A ();
if (A.getclass () ==a.class)
System.out.println ("equal");
else System.out.println ("unequal");
The result is a print "equal".
As you can see, object A is an instance of a, a class where the result of using A.getclass () in an If statement is the type class of a, and in Java it means that a type class of a particular type can be obtained by using the "type. Class" method, because A.getclass () Get the type of a class, which is a.class, so the result of the above code execution is to print out "equal". It is particularly important to note that the type class is one by one corresponding to the type class of the parent class and the subclass is different, so if a is a subclass of B, then the following code will get the output of "unequal":
A = new A ();
if (A.getclass () ==b.class)
System.out.println ("equal");
else System.out.println ("unequal");
So, if you know an instance, you can get the type class of the object by using the "GetClass ()" Method of the instance, and if you know a type, you can use the ". Class" method to get the type class for that type.
2. Get the type of information
After you get the type class, you can call some of these methods to get the type of information, the main methods are:
GetName (): String: Gets the full name of the type.
Getsuperclass (): class: Gets the direct parent of the type, and returns null if the type does not have a direct parent class.
Getinterfaces (): class[]: Gets all interfaces implemented by the type.
IsArray (): Boolean: Determines whether the type is an array.
Isenum (): Boolean: Determines whether the type is an enumeration type.
Isinterface (): Boolean: Determines whether the type is an interface.
Isprimitive (): Boolean: Determines whether the type is a basic type, that is, int,boolean,double, and so on.
IsAssignableFrom (Class CLS): Boolean: Determines whether this type is a parent (ancestor) class or parent (ancestor) interface of type CLS.
Getcomponenttype (): Class: If the type is an array, then the component type of the array is returned.
You can also perform type conversion operations such as:
Assubclass (class Clazz): Class: Converts this type
1 The Obejct class has a getclass () method:2 returns the run-time class for this Object. 3The class object that is returned is represented by theStatic synchronizedmethod to lock the object. 4 5 Public classArt {6 Art () {7System.out.println ("Art");8 System.out.println (GetClass (). GetName ());9 }Ten } One A Public classDrawingextendsArt { - Drawing () { -System.out.println ("Drawing"); the System.out.println (GetClass (). GetName ()); - } - } - + Public classCartoonextendsdrawing{ - Cartoon () { +System.out.println ("Cartoon"); A System.out.println (GetClass (). GetName ()); at } - - Public Static voidMain (string[] args) { -Cartoon x =NewCartoon (); - -Drawing one =(Drawing) x; inArt(ART) x; - if(One = =Both ) { toSystem.out.println ("= ="); +}Else { -SYSTEM.OUT.PRINTLN ("! ="); the } * System.out.println (x.tostring ()); $ System.out.println (one.tostring ());Panax Notoginseng System.out.println (two.tostring ()); - } the } + //Output A Art the Com.cignacmc.knowledge.inheritance.Cartoon + Drawing - Com.cignacmc.knowledge.inheritance.Cartoon $ Cartoon $ Com.cignacmc.knowledge.inheritance.Cartoon -== - [email protected] the [email protected] - [email protected]Wuyi the conclusion: When calling GetClass (), returns the Real class object of this object. - The equivalent of 3 inherited objects and the output shows that the three objects have the same this pointer, that is, the memory address is consistent. WuGetClass () returns the most authentic class object represented by this pointer, which is the topmost subclass.
The GetClass () function in Java