Java reflection-call constructor and Operation Array

Source: Internet
Author: User

The two share a person class. Please refer to the code ..

The first is the demo of calling the constructor through Java reflection.

Package COM. jadyer. reflection; </P> <p> Import Java. lang. reflect. constructor; </P> <p>/** <br/> * reflection call constructor <br/> */<br/> public class invokeconstructor {<br/> Public static void main (string [] ARGs) throws exception {<br/> class <?> Clazz = Class. forname ("com. jadyer. model. person "); // create a class object, used to describe the person class </P> <p>/** <br/> * call the construction method with parameters <br/> */<br/> class <?> [] Argtypes = {string. Class, integer. Class}; // specifies the parameter of the constructor to be called. <br/> constructor <?> Constructor = clazz. getconstructor (argtypes); // obtain the constructor of a specified parameter. <br/> Object OBJ = constructor. newinstance ("Tomb stealing notes", 888); // creates an object based on the given real parameters. The returned value is the handle of the newly created object. <br/> system. out. println (OBJ); // print the newly created object. Here, tostring () is automatically called () output the values of the two attributes </P> <p>/** <br/> * call the construction method without parameters <br/> * @ see can also use the following two method, obtain the construction method without parameters <br/> * @ see constructor = clazz. getconstructor (new class [0]) <br/> * @ see constructor = clazz. getconstructor (null) <br/> * @ see. You can also use OBJ = clazz. newinstance () creates an object without a constructor <br/> */<br/> OBJ = clazz. getconstructor (). newinstance (); // obtain the construction method without parameters and use it to create an object <br/> system. out. println (OBJ); // print the attribute value of the newly created object. The tostring () method is still called automatically here <br/>}< br/>}

Then there is the shared person class.

Package COM. jadyer. model; </P> <p> public class person {<br/> Public string name; <br/> Public integer age; </P> <p> Public Person () {}</P> <p> Public Person (string name, integer age) {<br/> This. name = Name; <br/> This. age = age; <br/>}</P> <p> @ override <br/> Public String tostring () {<br/> return "Name: "+ name +"/Tage: "+ age; <br/>}< br/>}

Finally, the demo that uses Java reflection to operate Arrays

Package COM. jadyer. reflection; </P> <p> Import Java. lang. reflect. array; </P> <p> Import COM. jadyer. model. person; </P> <p>/** <br/> * array of reflection Operations <br/> */<br/> public class processarray {<br/> Public static void main (string [] ARGs) {<br/> int [] records = {89, 56, 43, 77, 33}; <br/> person [] persons = {new person ("Zhang qiling ", 26), new person ("Wang Zang Hai", 36), new person ("Tie bin Sheng", 46) }; <br/> records = (INT []) incrementarray (records ); <Br/> Persons = (person []) incrementarray (persons); </P> <p> List (records); // The five rows output here are zero, is the default initialization value of the expanded array <br/> system. out. println ("-------------------------------------"); </P> <p> List (persons); // The three rows output here are null, is the default initialization value of the expanded array <br/> system. out. println ("-------------------------------------"); </P> <p> int [] [] AA = (INT [] []) array. newinstance (Int. class, 2, 3); // create a multi-dimensional array and specify the element type and length of each dimension. <br/> system. out. println (AA ); <Br/>}</P> <p>/** <br/> * doubles the size of the specified array, at the same time, save the elements of the original array to the new array after expansion <br/> */<br/> Public static object incrementarray (Object array) {<br/> class <?> Elementtype = array. getclass (). getcomponenttype (); // obtain the type of the array element <br/> int size = array. getlength (array); // obtain the original size of the array <br/> Object newarray = array. newinstance (elementtype, size * 2); // creates an Array Based on the specified Element type and capacity. <br/> // system. arraycopy (array, 0, newarray, 0, size); // assign an array element. The following is a better description of the reflection mechanism <br/> for (INT I = 0; I <size; I ++) {// copy array elements <br/> Object OBJ = array. get (array, I); // gets the I-th element in the array, and returns the object type <br/> array. set (newarray, I, OBJ); // set the value of element I of the target array newarray to OBJ <br/>}< br/> return newarray; <br/>}</P> <p>/** <br/> * display all elements in the specified array <br/> */<br/> Public static void list (Object array) {<br/> int size = array. getlength (array); // obtain the length of the specified array object using the reflection API <br/> for (INT I = 0; I <size; I ++) {<br/> system. out. println (array. get (array, I); // output each element in the array <br/>}< br/>}

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.