Essay Java Reflection mechanism

Source: Internet
Author: User

One: Reflection mechanism

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 dynamically obtained information and the function of the method of the dynamic call object A reflection mechanism called the Java language.

Second: The function of the reflection mechanism

1 anti-compilation:. Class-->.java

2 through the reflection mechanism accesses the Java object property, the method, constructs the method and so on;

Three:Sun gives us the classes in the reflection mechanism:

Java.lang.Class;

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

Java.lang.reflect.Method;

Java.lang.reflect.Modifier;

Four: Concrete Implementation

1 reflection mechanism get class there are three ways to get the Employee type

1 //The first way:2Class C1 =class.forname ("Employee"); 3 //The second way:4 //each type in Java has a class attribute. 5CLASSC2 =Employee.class; 6 //The Third Way:7 //any Java object in the Java language has a GetClass method8Employee e =NewEmployee (); 9CLASSC3 =E.getclass ();//C3 is the runtime class (the run-time class for E is employee)

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

1 Class C =class.forname ("Employee");   2 // Create a new instance  of the class represented by this class object 3 c.newinstance () // an employee's parameterless construction method was called.  

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

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

//get the entire classClass C = class.forname ("Java.lang.Integer"); //get all the properties            field[] fs = C.getdeclaredfields (); //defines a variable-length string used to store propertiesStringBuffer SB =NewStringBuffer (); //stitching each property into this string by appending the method//the outermost public definition            sb.append (modifier.tostring (C.getmodifiers ()) + "class" + c.getsimplename () + "{\ n");//Get Modifier public class /c0>//each of the properties inside             for(Field field:fs) {sb.append ("\ t");//Space
          sb.append (modifier.tostring (Field.getmodifiers ()) + " ");
// Gets the modifier for the property , such as public,static, and so onSb.append (Field.gettype (). Getsimplename ()+ " ");//the name of the type of the propertySb.append (Field.getname ()+ "; \ n");//Name of property + carriage return} sb.append ("}"); System.out.println (SB);

b, get a specific property:

1  Public Static voidMain (string[] args)throwsexception{2<span style= "White-space:pre" > </span>//the previous way:3     /* 4 User U = new user ();5 u.age = n;//set6 System.out.println (u.age);//get7     */               8     //Get class9Class C = class.forname ("User"); Ten     //Get ID Property OneField IdF =C.getdeclaredfield ("id");  A     //instantiate this class to assign O -     Object o = c.newinstance ();  -     //Break Package the     idf.setaccessible (true); //using the reflection mechanism can break the encapsulation and cause the properties of the Java object to be unsafe.  -     //assigns the id attribute of the O object to "Max" -Idf.set (O, "110");//Set -     //Get + System.out.println (Idf.get (o));  -}

4, get the method, and the construction method, no longer described in detail, just look at the keyword:

Essay Java Reflection mechanism

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.