Java reflection learning getClass () function application

Source: Internet
Author: User

Java reflection Learning
Reflection can be understood as an operation to obtain object type information during runtime. Traditional programming methods require programmers to determine the type used during the compilation phase. However, with the help of reflection, programmers can dynamically obtain the information and write more portable code. Strictly speaking, reflection is not a feature of a programming language, because reflection can be implemented in any language. However, if the programming language itself supports reflection, the reflection implementation will be much more convenient.

1. Obtain the type class
We know that everything in Java is an Object. The objects we use generally inherit directly or indirectly from the Object class. The Object class contains a method named getClass, which can be used to obtain an instance type class. A type class represents a type class, because everything is an object and the type is no exception. in Java, a type class is used to represent a type. All types are Class instances. For example, you have the following code:
A a = new ();
If (a. getClass () = A. class)
System. out. println ("equal ");
Else System. out. println ("unequal ");
The result is "equal ".
We can see that object a is an instance of A, A class, and. the result returned by getClass () is exactly the type class of A. in Java, it indicates that A type class of A specific type can use "type. class, because. getClass () is the type class of A, that is,. class, so the result of the above code execution is to print "equal ". Note that the type classes correspond one by one. The type classes of the parent class are different from those of the subclass. Therefore, assume that A is A subclass of B, the following code will get the output of "unequal:
A a = new ();
If (a. getClass () = B. class)
System. out. println ("equal ");
Else System. out. println ("unequal ");
Therefore, if you know an instance, you can use the "getClass ()" method of the instance to obtain the type class of the object. If you know a type, you can use ". class.

2. Obtain the type information.
After obtaining the type class, you can call some of the methods to obtain the type information. The main methods include:
GetName (): String: Get the full name of this type.
GetSuperClass (): Class: Obtain the direct parent Class of this type. If this type does not have a direct parent Class, null is returned.
GetInterfaces (): Class []: obtains all interfaces implemented by this type.
IsArray (): boolean: determines whether this type is an array.
IsEnum (): boolean: determines whether the type is an enumeration type.
IsInterface (): boolean: determines whether this type is an interface.
IsPrimitive (): boolean: determines whether the type is of the basic type, that is, whether it is int, boolean, double, and so on.
IsAssignableFrom (Class cls): boolean: determines whether this type is a cls parent Class or parent interface.
GetComponentType (): Class: If this type is an array, the component type of this array is returned.
In addition, you can perform operations such as type conversion. The main methods are as follows:
AsSubclass (Class clazz): Class: set this type

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.