Dark Horse Programmer--java Learning 16 (Zhang Xiaoxiang)--reflection

Source: Internet
Author: User

------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------

Reflection mechanism

In a running state, for any class, you can know all of its methods and properties,

For any object, you can call any of its methods and properties

is to map the various components in the Java class into the corresponding Java classes

The underlying class of reflection

The instance object of class is the byte code, which is the binary data code that the class is compiled into.

Get an object in three different ways

1. Class name

2, object. GetClass

3, Class.forName ("Class name")

1  Packagelearn;2 3  Public classReflecttest {4      Public Static voidMain (string[] args)throwsexception{5String str1 = "abc";6Class CLS1 =Str1.getclass ();7Class cls2 = String.class;8Class CLS3 = Class.forName ("java.lang.String");9System.out.println (CLS1==CLS2);//trueTenSystem.out.println (CLS2==CLS3);//true One         //is the base type byte code ASystem.out.println (Cls1.isprimitive ());//false -System.out.println (int.class. isprimitive ());//true -System.out.println (int.class==integer.class);//false theSystem.out.println (int.class==integer.type);//true represents the byte code of the underlying type that it wraps -System.out.println (int[].class. Isprimitive ()/*IsArray is True*/);//false Array is not primitive type -          -     } +}

1, constructor class--Get the construction method

T newinstance (Object ... initargs)

Use the constructor method represented by this Constructor object to create a new instance of the Declaration class for the constructed method, and initialize the instance with the specified initialization parameters.

The class also has, but is not a parameter, and constructor can use a parameter, useful more

1         Constructor Con1 = String. class. GetConstructor (StringBuffer.  Class); // The source uses the class array to make the arguments 2         // byte-code strong-to-string = new string (New StringBuffer ("abc")); 3         String str2 = (string) con1.newinstance (new StringBuffer ("abc")); 4         // The second character of the output 5         System.out.println (Str2.charat (2));

2. Field class--Get member variables

byte code with = = Comparison

1 Changestringvalue (PT1);2 System.out.println (PT1);3     }4 5     Private Static voidChangestringvalue (Object obj)throwsIllegalArgumentException, illegalaccessexception {6         //Remove all members of the class into the field array7field[] Fields =Obj.getclass (). GetFields ();8          for(Field field:fields)9         {Ten //if (Field.gettype (). Equals (String.class)) because the bytecode object is unique One             if(Field.gettype () ==string.class) A             { -String OldValue =(String) field.get (obj); -String newvalue = oldvalue.replace (' B ', ' a '); the                 //To reassign a value to an object by using the Set method - field.set (obj, newvalue); -             } -              +         } -          +     } A}

3. Method Class--Obtaining methods

Represents a static method when the first argument passed by the Invoke method is null

1 //Testarguments.main (New string[]{"111", "222", "333"});2String Startingclassname = args[0];3Method Mainmethod = Class.forName (startingclassname). GetMethod ("main", string[].class);4 //Mainmethod.invoke (null,new object[]{new string[]{"111", "222", "333"});5         //All in case of a 1.5 feature unpacking.6Mainmethod.invoke (NULL, (Object)Newstring[]{"111", "222", "333"});7 8 9 classtestarguments{Ten      Public Static voidMain (string[] args) One     { A          for(String Arg:args) -         { - System.out.println (ARG); the         } -     } -}

4. Reflection in an array

An array with the same number of dimensions and element type, the same class

class<? Super T> Getsuperclass ()

Returns the byte code of the parent class of this method

This method is called through the byte code of the array, and the byte code of the object is obtained.

5, the role of reflection--frame

The house sells to the customer, the user installs the windows and doors,

The house I make is the frame.

The tool class is called by the user's class, while the framework calls the user-supplied class

Dark Horse Programmer--java Learning 16 (Zhang Xiaoxiang)--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.