实体类:[/color]
public class Hotel {
private long id;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}}
测试方法:
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});
}出错信息
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)
慕莱坞森
侃侃无极
蝴蝶刀刀
眼眸繁星
ABOUTYOU
随时随地看视频慕课网APP
相关分类