package test; import org.apache.commons.beanutils.PropertyUtils; public class Test { private int x; private int y; public int getX() { //get ,set 方法不要忘记 return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } Test(){ } Test(int x,int y){ this.x=x; this.y=y; } public static void main(String[] args) { Test t=new Test(3,8); try { Object k= PropertyUtils.getProperty(t, "x");//理解其反射原理 System.out.println(k.toString());//打印 值:3 } catch (Exception e) { e.printStackTrace(); } } } |
|
来自: 走墨 > 《java 个人总结》