Reflection application

xiaoxiao2021-03-06  83

By the following example, you can understand the use of Reflection.

Array extension

Writing a method Returns a length of twice the length of the array length, the content is copied, and the parameters required to input and output are Object types.

This may be implemented here with the reflection API of Java. The specific point is the Array class.

Public Object Expand (Object Array) {

IF (! Array.getClass (). Isarray ())

Return NULL;

Class ComponentType = array.getclass (). Getcomponenttype ();

INT Newlength = array.getlength (array) * 2;

Object newaRray = array.newinstance (ComponentType, Newlength);

System.Arraycopy (Array, 0, NewArray, 0, Array.getLength (array);

//System.Arraycopy, array.getlength (array), array.getlength (array);

Return Newarray;

}

In fact, it is just a few simple methods, it is not difficult. Of course, don't know if you know these methods.

difficult. But it is also the accumulation of this simple thing, forming a gap. The road to go is still long ...

转载请注明原文地址:https://www.9cbs.com/read-98392.html

New Post(0)