Dark Horse Program Ape--java high-tech--reflection

Source: Internet
Author: User
Tags stringbuffer

----------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 execution state, for a random class. All the properties and methods of this class can be known, for any arbitrary object. Can call its arbitrary methods and properties, such dynamic access to information and the ability to dynamically invoke the method of the object is called the Java language reflection mechanism. Java Reflection (emission) mechanism: "When the program executes, it agrees 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, C + +. Java,c# is not a dynamic language. But Java has a very prominent dynamic correlation mechanism: Reflection, used in Java refers to our ability to load, detect, and use the classes during compilation.

In other words, a Java program can load an execution to know the name of a class, learn its full construct (but not include the methods definition), and generate its object entity, 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:

The main point is the ability of a program to access, detect, and alter its own state or behavior. And can be based on the state of their behavior and results. Adjusts or changes the state and related semantics of the descriptive behavior described in the application.

Reflection is a powerful tool in Java. can make it very convenient for us to create flexible code. The code can be assembled again, without the need for source links between components. However, it is very expensive to use the reflection incorrectly.

Second, the effect of reflection mechanism:

1, decompile:. Class-->.java

2, use the reflection mechanism to access the properties of the Java object, methods. construction method, etc.;

This seems to be easier to understand some. Below we look at how to implement these features in detail.

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;

Very many methods in reflection, properties 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 just to help you understand, better hands-on operation, ability to improve your resolution.

1, there are three ways to get a class from the reflection mechanism. We're here to get the employee type

1. // the first way:

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

3. // Another way:

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

5. Classc2 = Employee. class;

6. // Third Way:

7. //java language any Java object has a getclass method

8. employeee = new Employee ();

9. Classc3 = E.getclass (); //C3 is the execution time class (e 's execution time class 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 non-parameter construction .

3, Get Properties: Divided into all properties and specified properties:

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

// 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. // Add each attribute to this string by appending the method

8. // outermost public definition

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

Each attribute in the // inside

One . for (Field field:fs) {

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

sb.append (modifier.tostring (Field.getmodifiers ()) +""); // gets the modifier for the property. Like public,static , etc.

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!

Dark Horse Program Ape--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.