Java Advanced-Reflection (Reflect)

Source: Internet
Author: User

First, class

1, the class is an object, the class is an instance object of the Java.lang.Class class, this object is called the class type of the class;

2. Any class is an instance object of class, and the expression of the classes:

(1) Class c1 = Int. Class;

(2) Class C2 = C1.getclass ();

(3) Class C3 =null; C3 = Class.forName ("Com.imooc.reflect.Foo");

3, any class has an implicit static member variable;

4. Create a class type to create an instance object of the class:

C1.newinstance ();//forced type conversions are required, provided that there is no parameter-based construction method

5, Class.forName ("The full name of the class") not only represents the class type, but also represents the dynamic loading class;

6, compile time load class is static load class, run time load class is dynamic loading class;

7, the creation of objects through new is a static load class, at compile time you need to load all the possible classes to use;

Second, the reflection of the method

1. How to obtain the method

The parameter list of the method name and method can only determine a method;

2, method of reflection operation

Method.invoke (object,fields);//The same effect as a direct call

3, why to use the method of reflection

Specifies a method name to invoke a method, such as obtaining its property value from a standard JavaBean property name

4, through the Class,method to understand the nature of generics:

The reflected operation is a compiled operation, and the generic type of the collection after compilation is de-generalized

Generics in Java are protected from erroneous input, only valid during the compilation phase, bypassing the compilation is not valid (bypassing the compilation bypasses generics)

Iii. Reflection of member variables

1. Get member Variable information

(1) The member variable is also an object, Java.lang.reflect.Field

(2) Field class encapsulates operations on member variables

(3) The GetFields () method Gets the information of all the public member variables

(4) Getdeclaredfields () Gets the information of the member variable declared by the class itself

Iv. emission of tectonic methods

1, the constructor is also an object, the operation of the constructor is encapsulated in Java.lang.Constructor

2, GetConstructors get all the public's construction method

3. Getdeclaredconstructors gets all the constructors

The constructor obtained in 4, 3, Constructor.getparametertypes () Gets the class type of the argument list

V. Loading mechanism of Java class

1, class name. GetName (), gets the entire class type of the class;

2, class name. Getsimplename (), Gets the class type name, without the package name

3. Getdeclaredmethods () Gets all the methods that the class declares itself

4, Method class, methods object, a member method is an object

5, Germethods () Gets the method of the class, takes all public functions, including inherited from the parent class, does not ask access permissions

6, Getreturntype () Gets the class type of the method's return value

7, Getparametertypes () the class type of the type that gets the argument list

Finally, attach the practice code of the Reflection section:

public class Reflecttest {public static void main (String args[]) {try {class Class1 = string.class;//Create Object Class1//class clas S2 = float.class;//Create Object class1field[] Field=class1.getfields ();//Get all member variables Class1 method[] Methods =class1.getmethods ( );//Get all methods of Class1 constructor[] constructor=class1.getconstructors ();//Get all the constructor methods of Class1 System.out.println ("====== =CLASS1 member Variable ====== "); for (Field Field2:field) {System.out.println (Field2.getname ());} System.out.println ("All Methods of =======class1 ======"); for (method M:methods) {//Traverse Class1 all Methods System.out.print (M.getname ( )+"("); Parameter[] Pms=m.getparameters (); for (Parameter P:pms) {//Traversal method parameter list System.out.print (P.gettype () + ",");} M.invoke (obj, args); System.out.println (")");} SYSTEM.OUT.PRINTLN ("All construction methods of =======class1 ======"); for (Constructor constructor2:constructor) {// Traverse all construction methods of the Class1 System.out.print (Constructor2.getname () + "("); parameter[] pm2 = Constructor2.getparameters (); for (Parameter pc:pm2) {//Traverse the constructor method's parameter list System.out.print (Pc.gettype (). GetName () + ",");} SyStem.out.println (")");}} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}

  

Java Advanced-Reflection (Reflect)

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.