利用 Java Reflection 傳入字串陣例

 
String clazzName = "tw.webnode.client.Hello";
 
Class clazz = Class.forName(clazzName );
 
Class[] argTypes = new Class[] { String[].class };
 
Method method = clazz.getMethod("callHello", argTypes );
 
 
String[] hello = new String[2 ];
hello[0 ] = "hello1";
hello[1 ] = "hello2";
 
method.invoke(null, (Object) hello );
System.out.println(clazz.getName() );
String clazzName = "tw.webnode.client.Hello";
 
Class clazz = Class.forName(clazzName );
 
Class[] argTypes = new Class[] { String[].class };
 
Method method = clazz.getMethod("callHello", argTypes );
 
 
String[] hello = new String[2 ];
hello[0 ] = "hello1";
hello[1 ] = "hello2";
 
method.invoke(null, (Object) hello );
System.out.println(clazz.getName() );
 
 
 
 
public static void callHello(String[] params ) {
 
 
//System.out.println("傳入的 conn 為: " + conn );
System.out.println("傳入的 params 為: " + params.length );
}

 

© 2015 版權所有。

Create a website for freeWebnode