Dark Horse programmer--java High-tech-reflection

Source: Internet
Author: User

----------Android Training, Java training, Java Learning Technology blog, look forward to communicating with you! ------------

First, the concept of reflection

For the Java reflection mechanism is in the running state, for any class, can know all the properties and methods of this class, for any one object, can call any of its methods and properties; This dynamic acquisition of information and the ability to dynamically invoke the method of the object is called the reflection mechanism of the Java language. Java Reflection (emission) mechanism: "When a program is run, it is allowed to change the program structure or variable type, which is called Dynamic language." From this point of view, Perl,python,ruby is a dynamic language, and c++,java,c# is not a dynamic language. But Java has a very prominent dynamic-related mechanism: Reflection, in Java, refers to the classes that we can load, detect, and use completely unknown during compilation at runtime. In other words, a Java program can load a runtime to know the name of a class, learn its full construct (but not including the methods definition), and generate its object entities, or set values on its fields, or evoke its methods. ---------Official concept Two, for my personal understanding

First, take a look at the concept of reflection:

It is the ability of a program to access, detect and modify its own state or behavior, and to adjust or modify the state and related semantics of the behavior described by the application according to the state and result of its behavior.

Reflection is a powerful tool in Java that allows us to easily create flexible code that can be assembled again at runtime without the need for source code linking between components. But the use of reflection is costly.

Second, the effect of reflection mechanism:

1, decompile:. Class-->.java

2, through the reflection mechanism to access the Java object properties, methods, construction methods, etc.;

This seems to be easier to understand, and below we look at how to implement these features.

Three, let's take a look at sun. The classes in the reflection mechanism are provided for us:

Java.lang.Class;

Java.lang.reflect.Constructor; Java.lang.reflect.Field;

Java.lang.reflect.Method;

Java.lang.reflect.Modifier;

Many of the methods in reflection, attributes, and so on, we can query from these four classes. It is better to query the API documentation to better solve

In fact, the concept is only to help you understand, better hands-on operation, to improve your ability to solve.

1, the reflection mechanism gets the class there are three ways to get the employee type

1. // the first way:

2. Classc1 = Class.forName ("Employee");

3. // second way:

4. Each type in//java has a class attribute .

5. Classc2 = Employee. class;

6. // Third Way:

7. Any java object in the//java language has a getclass method

8. employeee = new Employee ();

9. Classc3 = E.getclass (); //C3 is the run-time class ( The run-time class for E is Employee)

2, create object: Get the class later we'll create its object, using newinstance:

1. Class C =class.forname ("Employee");

2.

3. // create A new instance of the class represented by this class object

4. objecto = C.newinstance (); // called the Employee method of constructing without parameters .

3, gets the attribute: is divided into all attributes and the specified properties:

A, let's look at the notation for all the attributes:

// get the entire class

1. Class C = class.forname ("Java.lang.Integer");

2. // get all the properties ?

3. field[] fs = C.getdeclaredfields ();

4.

5. // define variable-length strings for storing properties

6. stringbuffer sb = new stringbuffer ();

7. // stitch each property into this string by appending the method

8. // outermost public definition

9. sb.append (modifier.tostring (C.getmodifiers ()) + "class" + c.getsimplename () +"{\ n ");

Each of the properties inside

One . for (Field field:fs) {

sb.append ("\ t"); // Space

sb.append (modifier.tostring (Field.getmodifiers ()) +""); // gets the modifier for the property, such as Public , Static wait

Sb.append (Field.gettype (). Getsimplename () + ""); // the name of the type of the property

Sb.append (field.getname () +"; \ n"); // the name of the property + Enter

. }

.

sb.append ("}");

.

System.out.println (SB);

A little progress every day, a big step forward in success, fighting!

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Dark Horse programmer--java High-tech-reflection

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.