Java reflection mechanism

Source: Internet
Author: User
Reflection: You can dynamically obtain the members of a specified class and create class objects. Advantage: It improves program scalability. Zhang San, Li Si, and other objects in life can be described in the form of person. After the class file is generated, these class files are also things in life, so these class files can also be described. The corresponding type of the description is class. In Java, each bytecode file has a class object corresponding to it. Not only the referenced data type, but also the basic data type. Int. class string. class ---- class class1 = string. class; string S = "ABC"; Class class2 = S. getclass (); Class class3 = Class. forname ("Java. lang. string "); the preceding three methods are used to obtain bytecode files. Previously, the object was operated with the New Keyword, and the person P = new person () was called using the object. Member method; // 1. Load the person. Class file. // 2. initialize the object. P. function ("Heihei"); // when the object calls the member method, two factors need to be clarified: 1. object 2. you can use the bytecode object to obtain the parameter list. // Load person. class file, get person. class Object Class clazz = Class. forname ("person"); // creates and initializes a specified class through a bytecode file object. Person P = (person) clazz. newinstance (); // call a member through an object // P. function ("Heihei"); the class can be passed as a parameter, so the method can also be passed as a parameter, because the method exists in the bytecode file, therefore, you can use the class object to obtain the content in the bytecode file. Method M = Class. getmethod ("function", String. class); M. invoke (P, "Heihei"); classes or methods are passed as parameters. What are the advantages of this method for development? Requirement: design a motherboard. In order to improve the scalability in the later stage, that is, to improve the computer's capability in the later stage, provide the PCI interface. To facilitate the expansion of computer functions. Interface PCI {void open (); void close ();} class mainboard {public void usepci (pCI p) {P. open (); p. close () ;}} class mainboarddemo {public static void main (string [] ARGs) {mainboard MB = new mainboard (); // MB. usepci (null); MB. usepci (netcard) ;}} later, to expand computer functions, you need to add a nic, you only need to define a NIC class to implement the PCI interface, as long as the rule is overwritten, the motherboard can use this board. Class netcard implements PCI {public void open (); {system. out. println ("open");} public void close (); {system. out. println ("close") ;}} in order to use its Nic, you also need to perform a step in the defined application to create a NIC object and pass it as a parameter, then, modify the original program. This is not conducive to program robustness. Can these subclass objects appear after running without modifying the source code? As long as you create an object in the later stage of the design, you do not need to create an object in the later stage of the subclass object. You only need to inform the subclass name. In order to obtain the post-object and use it in the early stage, or provide a configuration file for external users. You can directly operate on the configuration file in the preliminary program. In the later stage, you only need to store the subclass name in the configuration file. In this case, you need to dynamically obtain the specified class and use the reflection mechanism to create objects in advance. Modify the application. Class mainboarddemo {public static void main (string [] ARGs) {mainboard MB = new mainboard (); file = new file ("conf.txt "); bufferenreader buff = new bufferenreader (New filereader (File); string classname = buff. readline (); Class clazz = Class. forname (classname); pci P = (PCI) clazz. newinstance (); MB. usepci (p) ;}} configuration file conf.txt when the NIC or sound card appears later, you only need to save the full class name of this subclass to the configuration file, the source program does not need to be modified. In this example, we can understand that reflection brings a strong extension to our program. Scalability. Link:
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: 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.