Get an array type by Reflect

xiaoxiao2021-03-06  21

Kingfish 2005.3

Today I saw a problem about how to get an array type by reflection, I tried it, can be solved by the Array class.

Make a record.

code show as below:

Import java.lang.reflect. *;

/ ** * @Author kingfish * * Todo java reflect * / class mystring {public mystring (string s) {system.out.println ("in constructor of the mystring");}}

Public class test {/ ** * constructor * @Param i * / public testReflect (int i) {system.out.println ("just for test"); public void test () {} public void test (MyString S) ) {} Public void test (mystring [] args) {} 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); // Reflects the Test (MyString) method, and requires the MyString class to reflect by M = C.getMethod ("Test", new class [] {class.forname ("mystring")}; system. Out.println (m); // Reflecting the Test (MyString []) method, requires the mystring [] type also reflects the //aRray.newInstance return type array, does not generate instance M = C.getMethod ("Test" , new class [] {array.newinstance (class.forname ("mystring"), 1); system.out.println (m);

// passed "[lclassname;", pay attention: the semicolon can't forget! M = C.getMethod ("Test", new class [] {class.forname ("[lmystring;")}; system.out.println (m);}}

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

New Post(0)