Today I saw a question about how to get the array type by reflection, and try it out through the array class.
Make a note.
The code is as follows:
Import java.lang.reflect.*;
/** * @author Kingfish * * TODO java reflect */class mystring{public MyString (String s) {System.out.println (' in const Ructor of the MyString "); } }
public class Testreflect { /** * constructor * @param i */ public Tes Treflect (int i) { System.out.println ("Just for Test"); } public void Test () {& nbsp } public void Test (MyString s) { } public void Test (mystring[) args) { &NB sp; } public static void main (string[] args) throws exception{ Class C = class. forname ("Testreflect"); method m = C.getmethod ("Test", New class[]{} ); System.out.println (m); //Reflection Test ( MyString) method m = C.getmethod ("Test", New Class[]{mystring.class}); System.out.println (m); //Reflection Test (MyString) method, and requires that the MyString class also need reflection to get m = C.getmethod ("Test", new ClaSs[]{class.forname ("MyString")}); System.out.println (m); //Reflection Test (mystring[]) method, requiring mystring[] type to also reflect get // Array.newinstance returns an array of types that does not produce instances of the class m = C.getmethod ("Test", New Class[]{array.newinstance ( Class.forName ("MyString"), 1). GetClass ()}); System.out.println (M); }}
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.