java中如何使用发射机制进行反射类中类 本帖最后由 zhangww1985 于 2011-08-05 17:42:04 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;public class Test { public static class A { private int a = 111; public void print(){ System.out.println("In Test.A,a="+a); } public int getA() { return a; } } public void print() { System.out.println("In Test,a="+new A().getA()); } public static void main(String[] args) { try { Class test = Class.forName("Test"); Object o = test.newInstance(); Method[] m=test.getDeclaredMethods(); for(Method method : m) { if(method.getName().contains("print")) { method.invoke(o, null); } } Class testa = Class.forName("Test$A"); Object oa = testa.newInstance(); Method[] ma=testa.getDeclaredMethods(); for(Method methoda : ma) { if(methoda.getName().contains("print")) { methoda.invoke(oa, null); } } } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InstantiationException 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(); } } } 反射内部类,内部类的命名是包名.Outer$Inner.Class b = Class.forName("A"+"$B");Object B = b.newInstance(); 因为你的内部类不是static的,所以必须先new A,然后才能new内部类即非反射方式的的时候B b = new A().new B(); //是通过这样才生成B实例的同样道理,反射的时候也必须先new A才能new B Servlet问题,急求解决!!! String格式化 weblogic10.3 如何访问发布得项目 关与session的问题 Hibernate 问题 为啥大多数程序员到了30后就要转行呢 在web容器中是哪个类实现了HttpServletRequest这个接口 有关使用反射机制与JavaBean取数据记录并输出的问题。 菜鸟问题:关于hibernate的! 寻觅专业开发商:公司要在十天内上马一个BBS系统,有意者从速! 关于struts2上传遇到的问题 刚刚学完ssh2,想做个小项目练练,谁有给一份看看?
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Test {
public static class A {
private int a = 111;
public void print(){
System.out.println("In Test.A,a="+a);
}
public int getA()
{
return a;
}
}
public void print()
{
System.out.println("In Test,a="+new A().getA());
}
public static void main(String[] args)
{ try {
Class test = Class.forName("Test");
Object o = test.newInstance();
Method[] m=test.getDeclaredMethods();
for(Method method : m)
{
if(method.getName().contains("print"))
{
method.invoke(o, null);
}
}
Class testa = Class.forName("Test$A");
Object oa = testa.newInstance(); Method[] ma=testa.getDeclaredMethods();
for(Method methoda : ma)
{
if(methoda.getName().contains("print"))
{
methoda.invoke(oa, null);
}
}
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException 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();
}
}
}
即非反射方式的的时候
B b = new A().new B(); //是通过这样才生成B实例的
同样道理,反射的时候也必须先new A才能new B