Java Reflection Learning GetClass () function Application _java
Source: Internet
Author: User
Java Reflection Learning The so-called reflection, can be understood as the operation of getting the object type information during the run time. Traditional programming methods require programmers to decide which types to use at compile time, but with the help of reflection, programmers can dynamically get the information to write more portable code. In a strictly speaking sense, reflection is not a feature of a programming language, because reflection can be implemented in any language, but if the programming language itself supports reflection, then the implementation of reflection is much more convenient.
1, get the type class We know that everything in Java is an object, and the objects we normally use are inherited directly or indirectly from the object class. The object class contains a method named GetClass, which allows you to get the type class of an instance. A type class refers to a class that represents a type, because everything is an object, and the type is no exception, and the type class is used in Java to represent a type. All type classes are instances of class classes. For example, you have the following code:
A A = new A ();
if (A.getclass () ==a.class)
System.out.println ("equal");
else System.out.println ("unequal");
The result is to print out "equal".
As you can see, object A is an instance of a, a class, and the result returned by using A.getclass () in the IF statement is the type class of a, which in Java represents a specific type of type class that can be obtained in the form of "type. Class" Because A.getclass () Get a type class, which is a.class, so the result of the above code is to print out "equal". It is particularly noted that the type class is one by one corresponding, the type class of the parent class is different from the subclass class, so if a is a subclass of B, the following code will get the output of "unequal":
A A = new A ();
if (A.getclass () ==b.class)
System.out.println ("equal");
else System.out.println ("unequal");
So, if you know an instance, then you can get the type class of the object through the "GetClass ()" Method of the instance, if you know a type, then you can use the ". Class" method to get the type class of 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 class of the type, and returns null if the type does not have a direct parent class.
Getinterfaces (): class[]: Gets all interfaces implemented by this type.
IsArray (): Boolean: Determines whether the type is an array.
Isenum (): Boolean: Determines whether the type is an enumerated type.
Isinterface (): Boolean: Determines whether the type is an interface.
Isprimitive (): Boolean: Determines whether the type is the base type, that is, whether it 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, return the component type of the array.
In addition, you can do type conversion such operations, the main methods are:
Assubclass (class Clazz): Class: Converts this type
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service