14th. Type Information 1 (reflection of Java)

Source: Internet
Author: User

Reflection of the 1.java language

1  Packagecn.itcast_01;2 /*1. Get the construction method through reflection and use3 *//Get byte-code file Object4 Class C = class.forname ("Cn.itcast_01.Person");5 //Gets the specified constructor object (only acquired)6 The formal parameter of the method is the form parameter of the construction method. Class object (class<?> .... parametertypes)7 * The Parametertypes parameter is an array of Class objects8 Constructor con = c.getdeclaredconstructor (string.class);9 //using the constructor to create an instance object of the classTen //con.setaccessible (true); Canceling language access checks One Object obj = Con.newinstance ("Wind Qing"); A * 2. Get the member method by reflection and use - *//Get byte-code file Object - Class C = class.forname ("Cn.itcast_01.Person"); the Constructor con = c.getconstructor (); - Object obj = con.newinstance (); - //Public Method GetMethod (String name,class<?> .... parametertypes) - The first parameter represents the method name, and the second parameter represents the class type of the method's argument + //Get Member Method Object - Method m1 = C.getmethod ("show"); + method m2 = C.getmethod ("method", String.class); A //Call member methods through this object at M1.invoke (obj);//M1 method that invokes the Obj object - M2.invoke (obj, "hello");//M2 method that invokes the Obj object - * 3. Get member variables by reflection and use - *//Get a single member variable - * Field Addressfield = C.getfield ("address"); - *//Assign a value to the specified variable in * Addressfield.set (obj, "Beijing"); -  *  to *//Cancel language access check + * Addressfield.setaccessible (TRUE); - * Con.setaccessible (TRUE); the * M1.setaccessible (TRUE); *  */ $  Public classReflectdemo {Panax Notoginseng  -}

2. Get the constructor object and use the

1  Packagecn.itcast_01;2 3 ImportJava.lang.reflect.Constructor;4 5  Public classReflectDemo2 {6      Public Static voidMain (string[] args)throwsException {7         //Class public final class Class<t>extends Object8         //gets the byte-code file object for the class9         /*1.static class<?> forname (String className)Ten * 2.public final class<?> getclass () One * 3. For example, the type of String.class is class<string>.  A          */ -          //Get bytecode File object -Class C = class.forname ("Cn.itcast_01.Person"); the         //Get Constructor Method object -          /*1.constructor<t> getconstructor (class<?> ... parametertypes) - gets the public constructor object for the specified parameter - * 2. Constructor<?>[] GetConstructors () + get all public constructor objects - 3. Constructor<t> getdeclaredconstructor (class<?> ... parametertypes) + gets the constructor object for the specified argument A 4.constructor<?>[] getdeclaredconstructors () at get all constructor objects -           */ -         //Get Constructor Object -         //Constructor con = c.getconstructor ();//non-parametric construction -Constructor con = c.getdeclaredconstructor (String.class,int.class); -         //To call a construction method using a constructor object inObject obj = con.newinstance ("Zhang San", 22); -Person P = (person) con.newinstance ("Zhang San", 22); toSystem.out.println (obj);//Person [Name= Zhang San, age=22] +SYSTEM.OUT.PRINTLN (P);//Person [Name= Zhang San, age=22] -     } the}
3. Get the normal member method
1  Packagecn.itcast_01;2 ImportJava.lang.reflect.Constructor;3 ImportJava.lang.reflect.Method;4  Public classReflectDemo3 {5@SuppressWarnings ("Unchecked")6      Public Static voidMain (string[] args)throwsException {7         //gets the byte-code file object for the class8Class C = class.forname ("Cn.itcast_01.Person");9         //Get Constructor ObjectTenConstructor con =c.getdeclaredconstructor (); One         //Create a new instance AObject obj =con.newinstance (); -         /*get a member method object from a bytecode file object: - * 1.Method GetMethod (String name, Class<?> .... parametertypes) the * 2.method[] getmethods () - * 3.Method Getdeclaredmethod (String name, Class<?> .... parametertypes) - * Returns a method object that reflects the specified declared method of the class or interface represented by this class object.  - * 4.method[] getdeclaredmethods () + * Returns an array of method objects, including private and public methods, except for inheritance -          */ +         //method[] m = C.getdeclaredmethods (); A         //gets the member method object (Parameter list: Method name, method parameter of the. Class object) atMethod m1 = C.getdeclaredmethod ("Show"); -         //using member method Objects -M1.invoke (obj);//Show Method -         //Get Member Method Object -Method m2 = C.getdeclaredmethod ("Method", String.class); -         //Use this method inM2.setaccessible (true); -M2.invoke (obj, "Zhang San");//method of Zhang San to System.out.println (M2.getname ()); +     } -}
4. Get Field Properties
1  Packagecn.itcast_01;2 ImportJava.lang.reflect.Constructor;3 ImportJava.lang.reflect.Field;4 5  Public classReflectDemo4 {6      Public Static voidMain (string[] args)throwsException {7         //gets the byte-code file object for the class8Class C = class.forname ("Cn.itcast_01.Person");9         //Get Constructor ObjectTenConstructor con = c.getdeclaredconstructor ();//no parameter constructor One         //Create a new instance of the class (using the parameterless constructor) AObject obj =con.newinstance (); -         //Get Field Object -         /*1.Field Getdeclaredfield (String name) the * 2.field[] getdeclaredfields () - * 3.Field GetField (String name) - * 4.field[] getfields () -          */ +Field NameField = C.getdeclaredfield ("name"); -Field Agefield = C.getdeclaredfield ("Age"); +         //Assigning a value to a Field object (object must be set) ANamefield.setaccessible (true); atAgefield.setaccessible (true); -Namefield.set (obj, "Zhang San"); -Agefield.set (obj, 22); -System.out.println (obj);//Person [Name= Zhang San, age=22] -     } -}

14th. Type Information 1 (reflection of Java)

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.