62,615
社区成员
发帖
与我相关
我的任务
分享
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Test5
{
public static void main(String[] args)
{
String[] printObj = new String[]{"a","b","c"};
A a = new A();
a.test(printObj); //第一次调用test
a.test("d","e","f"); //第二次调用test
try {
Method testMethod = a.getClass().getDeclaredMethod("test", String[].class);
testMethod.invoke(a, printObj); //第三次调用test
testMethod.invoke(a, "d", "e", "f"); //第四次调用test
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class A
{
void test(String... printObj)
{
for(String x : printObj)
{
System.out.println(x);
}
}
}
class A
{
void test2(String str) {
System.out.println(str);
}
void test(String ...printObj)
{
for(String x : printObj)
{
System.out.println(x);
}
}
}
Method testMethod = a.getClass().getDeclaredMethod("test2", String.class);
testMethod.invoke(a, new Object[] {"rvalue"});
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Test5
{
public static void main(String[] args)
{
String[] printObj = new String[]{"a","b","c"};
A a = new A();
a.test(printObj); //第一次调用test
a.test("d","e","f"); //第二次调用test
try {
Method testMethod = a.getClass().getDeclaredMethod("test", String[].class);
testMethod.invoke(a, new Object[] {printObj}); //第三次调用test
testMethod.invoke(a, new Object[] {new String[] {"d", "e", "f"}}); //第四次调用test
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class A
{
void test(String ...printObj)
{
for(String x : printObj)
{
System.out.println(x);
}
}
}