{
private int x;
public int y;
public ReflectPoint(int x,int y)
{
this.x = x;
this.y = y;
}
}
class Test1
{
public static void main(String[] args)
{
try{
ReflectPoint rp = new ReflectPoint(3,5);
Field fieldY = rp.getClass().getField("y");
System.out.println(fieldY.get(rp));
}//没有捕获异常
catch (Exception e) {
// TODO: handle exception
System.out.println(e);
}
}
}
反射能够获取到类的私有构造函数,成员变量和方法,并对其进行操作。作者: IT人 时间: 2014-1-9 14:55
根据你的代码我运行了一下!抛异常后没有错呀,你【抛出异常试试。
public class ReflectPoint {
private int x;
public int y;
public ReflectPoint(int x,int y)
{
this.x = x;
this.y = y;
}
}
import java.lang.reflect.Field;
public class ReflectText {
public static void main(String[] args)throws Exception {
// TODO Auto-generated method stub
ReflectPoint rp = new ReflectPoint(3,5);
Field fieldY = rp.getClass().getField("y");