Getting the array type through reflect

Source: Internet
Author: User
Tags array reflection string
Array
Kingfish 2005.3

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); }}


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.