[color=red]实体类:[/color]
public class Hotel {
private long id;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
[color=red]测试方法:[/color]
public void testReflect() throws Exception{
Object object = Hotel.class.newInstance();
String setMethod = "setId";
Method method = object.getClass().getDeclaredMethod(setMethod, new Class[]{Object.class});
method.invoke(object, new Object[]{1L});
}
[color=red]出错信息:[/color]
java.lang.NoSuchMethodException: setId
at java.lang.ClassCache.findMethodByName(ClassCache.java:247)
at java.lang.Class.getDeclaredMethod(Class.java:731)
at java.lang.reflect.Method.invokeNative(Native Method)