62,612
社区成员
发帖
与我相关
我的任务
分享
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;
}
}