Reflection learning and reflection decoupling applications in Java

Source: Internet
Author: User

From: http://student.csdn.net/space.php? Uid = 972460 & Do = Blog & id = 42231

Reflection is one of the features of Java programming language. It allows dynamic discovery and binding of classes, methods, fields, and all other elements generated by languages. Reflection does not simply list classes, fields, and methods. Through reflection, you can also create instances, call methods, and access fields at runtime. Reflection is the key to Java being regarded as a dynamic (or quasi-dynamic) language.

In summary, the Java reflection mechanism mainly provides the following functions.

1. Determine the class to which any object belongs during running;

2. Construct the object of any class at runtime;

3. Determine the member variables and methods of any class at runtime;

4. Call the methods of any object at runtime. Through reflection, you can even call the private method;

5. Generate a dynamic proxy.

There are not many classes required for Java reflection, mainly including Java. lang. class and Java. lang. the field, constructor, method, and array classes in the reflect package provide a simple description of these classes.

1. Class class: an instance of the class indicates the classes and interfaces in the running Java application.

2. Field Class: provides information about the attributes of a class or interface, as well as its dynamic access permissions. The reflected field may be a class attribute or instance attribute. A simple understanding can regard it as a class that encapsulates the attributes of the reflection class.

3. constructor class: Provides information about a single constructor of the class and its access permissions. This class is different from the field class. The field class encapsulates the attributes of the reflection class, while the constructor class encapsulates the constructor method of the reflection class.

4. method class: Provides information about a separate method on a class or interface. The reflected methods may be class methods or instance methods (including abstract methods ). This class is not hard to understand. It is a class used to encapsulate reflection class methods.

5. array class: provides static methods for dynamically creating arrays and accessing arrays. All methods in this class are static methods.

Among them, the class is the origin of Java reflection. For any class you want to explore, you can obtain other desired information through the class object only by generating a Class Object for it. Next, we will focus on the class.

In actual development, in order to strip the dependency between various classes (that is, decoupling), when calling and instantiating classes, configure the corresponding class name in the configuration file, read the class name in the program, and then load and instantiate the class in the program through reflection technology, such as common database driver classes, to enable the dependency on a specific database Driver Class, place the used database driver class name in the configuration file (commonly used XML files, properties files, and text files) and load the driver in the program, to achieve database decoupling, that is to say, as long as you modify the configuration file, you can easily change the database type. The following describes the properties configuration file in detail.

Example:Write programs and use configuration files for loose coupling.

Testreflectmain class

Import Java. AWT. *; </P> <p> Import Java. AWT. event. *; </P> <p> Import javax. swing. *; </P> <p> public class testreflectmain extends jframe implements actionlistener {</P> <p> private javax. swing. jbutton B1; </P> <p> Public testreflectmain () {</P> <p> b1 = new javax. swing. jbutton ("click"); </P> <p> This. setlayout (New flowlayout (); </P> <p> b1.addactionlistener (this); </P> <p> This. add (B1); </P> <p> This. setsize (400,400); </P> <p> This. setvisible (true); </P> <p >}</P> <p> Public void actionreceivmed (actionevent E) {</P> <p> operateproperties = new operateproperties (); </P> <p> string classname = </P> <p> operateproperties. getvaluebypropertyname ("testf. properties "," class "); </P> <p> try {</P> <p> jframe F = (jframe) class. forname (classname ). newinstance (); </P> <p >}catch (exception ex) {</P> <p> javax. swing. joptionpane. showco Nfirmdialog (this, "generation failed! "); </P> <p >}</P> <p> Public static void main (string [] ARGs) {</P> <p> New testreflectmain (); </P> <p >}</P> <p> class F1 extends jframe {</P> <p> Public F1 () {</P> <p> super ("this is F1"); </P> <p> This. setlayout (New flowlayout (); </P> <p> jlabel L = new jlabel ("F1 is displayed now"); </P> <p> This. add (l); </P> <p> This. setsize (200,200); </P> <p> This. setvisible (true ); </P> <p >}</P> <p> class F2 extends jframe {</P> <p> Public F2 () {</P> <p> super ("this is F2"); </P> <p> This. setlayout (New flowlayout (); </P> <p> jlabel L = new jlabel ("display class F2"); </P> <p> This. add (l); </P> <p> This. setsize (200,200); </P> <p> This. setvisible (true); </P> <p >}</P> <p>

Define a file testf. properties in the folder where the source program file is located. The file content is as follows:

Class = F2

Then run the program. When you click the button, the result 1 is displayed.

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.