import java.lang.reflect.*;public class MyJReflect{
static Person p ;
public static void main(String[] args) {
try{
p = new Person();
System.out.println(1);
Method m = p.getClass().getMethod("setAge", Integer.class);
System.out.println(2);
m.invoke(p, new Object[]{new Integer(25)});

System.out.println(p.getAge());
System.out.println(3);

}catch(Exception e){}
}
}class Person{
private String name;
private int age;

public Person(){

}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}

public String show(){
return this.name + ":" + this.age;
}
}
只能输出1,输出一之后就没执行了。

解决方案 »

  1.   

    catch(Exception e){}
    这里,不要装样子啊,看看捕获什么错误了............
      

  2.   

    java.lang.NoSuchMethodException: com.hdj.reflect.Person.setAge(java.lang.Integer)
    at java.lang.Class.getMethod(Unknown Source)
    at com.hdj.reflect.MyJReflect.main(MyJReflect.java:11)
      

  3.   

       Method m = p.getClass().getMethod("setAge", Integer.class);
    这句异常了吧 直接跳到catch了
      

  4.   

    呵呵 最开始最好在catch快加上e.printStackTrace()  把异常捉住。
      

  5.   

    Method m = p.getClass().getMethod("setAge", Integer.class);
    这句抛出异常了,然后这句之后的没执行,直接跳到了catch中。
      

  6.   

    肯定是程序报错了,而楼主又写了try{}catch(Exception e){}。当捕获到错误时,又没做任何处理。当然什么也不显示了。如果用到了异常捕获,最好打印一下错误信息。
      

  7.   

    Method m = p.getClass().getMethod("setAge", Integer.class);
    Integer.class改为int.class这个要与下面对应,,,下面是int就是int.class
    下面的函数是Integer就是Integer.class