Java reflective _javascript techniques in the learning of communication and intelligence podcast

Source: Internet
Author: User
Tags reflection
In addition, Mr. Beebe explained the reflection mechanism for us, although in the future we will not necessarily involve reflection in the development process, but reflection does Java a powerful tool that enables us to create flexible code that can be assembled at runtime without the need for a source representation link between components. For example, in the Tomcat class loader This mechanism is used by many frameworks.
One, what is reflection
The concept of reflection was first proposed by Smith in 1982, mainly referring to the ability of a program to access, detect, and modify its state behavior. Reflection is one of the characteristics of Java Program Development language, it also allows the running Java program to check itself, or "self audit", and can directly manipulate the internal properties of the program.
Classes required for Java reflection
1.Class class: its instance represents classes and interfaces in a running Java application
2.Constructor class: Provides information about a single construction method of a class and access to him.
3.Method class: Provides information about a separate method of a class or interface
4.Array class: Provides static methods for dynamically creating arrays or accessing arrays.
5.Field class: Provides information about the properties of a class or interface, and its dynamic access rights.
Third, the function of reflection
We have listed the related classes above, so what can we do with the specific use of these classes?
1) Gets the class object of the specified classes
Method One, through the object. GetClass. (Example: Person P;class c=p.getclass ();)
Method two, through class class of Forname method (example: Class C=class.forname);)
Method Iii. gets (class C = int.class) by type name. class
2 constructs an object of any class at run time, as follows:
① creates its object based on the class name
② returns this newly created object
L Use the parameterless construction method with the Newinstance method of the class object only.
Class C=class.forname (class name); man p = (person) c.newinstance;
L Use the structured method with parameters as follows:
① gets the object of the specified class (IBID.)
② the constructor class object that satisfies the requirements of the specified parameter through the class object
Constructor cons = C.getconstructor (String.class)
③ invokes the Newinstance method of the specified constructor object, passing in the value of the parameter used for the pair, to create the object.
Person P = (person) cons.newinstance ("John");
3 A method that invokes any object at run time, and can even call the private method by reflection
① gets the object of the specified class (IBID.)
② create an instance on demand
③ obtains an instance of method by using the getdeclaredmethods of the class object (depending on the need), invoking the methods via the Invoke method
Method M=c.getdeclaredmethods ("SetName", String.class);
returnvalue = M.invoke (P, "Zhangsan");
Note: If this is a private method, you can first call the method object's Setaccessible (true) to cancel the security check for this method
4 Call the properties of any object at run time
① gets the object of the specified class (IBID.)
② create an instance on demand
③ obtains an instance of the field class by using the Getdeclaredfield of the class object (depending on the need), using the Set method to modify the value of the object and get the value of the object.
Field F =c.getdeclaredfield ("name");
F.set (P, "John"); System.out.println (F.get (p));
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.