Java reflection learns the getClass of function application

  • 2020-04-01 01:25:53
  • OfStack

Java reflection learning
Reflection can be understood as an operation to obtain information about the type of an object at run time. Traditional programming methods require the programmer to decide what type to use at compile time, but with the help of reflection, the programmer can obtain this information dynamically to write more portable code. Strictly speaking, reflection is not a feature of programming languages, because reflection can be implemented in any language, but if the programming language itself supports reflection, it is much easier to implement.

1, get the type class
We know that in Java everything is an Object, and the objects we typically use inherit directly or indirectly from the Object class. The Object class contains a method called getClass, which you can use to get an instance's type class. A type class is a class that represents a type, because everything is an object, and a type is no exception. In Java, a type class is used to represent a type. All type classes are instances of the Class Class. For example, here's the code:
A A = new A();
If (al-qeada etClass () = = A.c lass)
System. The out. Println (" equal ");
The else System. Out. Println (" unequal ");
The result is to print "equal."
As you can see, the object is an instance of a, a, a certain class, used in the if statement al-qeada etClass () returns the result of it is a type of class, in Java said a specific type of class type can be used "type. Class" way, because of al-qeada etClass () is a type of class, namely A.c lass, so the above code execution is to print out the result of the "equal". Pay special attention to the class type is one-to-one, type of the parent classes and subclasses of class type are different, therefore, assume that A subclass of A is B, so the following code will get the output of the "unequal" :
A A = new A();
If (al-qeada etClass () = = biggest lass)
System. The out. Println (" equal ");
The else System. Out. Println (" unequal ");
So, if you know an instance, you can get the type class of the object through the instance's "getClass()" method, and if you know a type, you can get the type class of that type using the ".class "method.

2. Get the type of information
After getting the type class, you can call some of the methods to get the type 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.
GetInterfaces ():Class[] : gets all the interfaces implemented by this type.
IsArray (): Boolean: determines if the type is an array.
IsEnum (): Boolean: determines whether the type is an enumerated type.
IsInterface (): Boolean: determines whether this type is an interface.
IsPrimitive (): Boolean: determines if the type is a primitive, int, Boolean, double, etc.
IsAssignableFrom (Class CLS): Boolean: determines whether this type is a parent (ancestor) Class or parent (ancestor) interface of type CLS.
GetComponentType ():Class: returns the component type of the array if the type is an array.
In addition, type conversion can also be done, the main methods are:
AsSubclass (Class clazz):Class: will this type

Related articles: