java 反射 报错:Attempt to get java.lang.Integer field "..." with illegal data type conversion to int

类: Integer id;

反射时:

Field f = User.class.getDeclaredField("id"); 

f.setAccessible(true);

int s = f.getInt(u);

这样会报错:

java.lang.IllegalArgumentException: Attempt to get java.lang.Integer field "..." with illegal data type conversion to int

改为:

int s = (Integer) f.get(u);

 

posted @ 2017-07-27 16:23  不知为何就叫呵呵  阅读(4014)  评论(0编辑  收藏  举报