4 回答

TA贡献2051条经验 获得超10个赞
Method method = object.getClass().getDeclaredMethod(setMethod, new Class[]{[color=red]long.class[/color]});

TA贡献1801条经验 获得超8个赞
public void testReflect() throws Exception{
Object object = Hotel.class.newInstance();
String setMethod = "setId";
Method method = object.getClass().getMethod(setMethod);
method.invoke(object, new Object[]{1L});
}

TA贡献1873条经验 获得超9个赞
public static void main(String[] args) throws Exception{
Object o = Hotel.class.newInstance();
String setMethod = "setId";
Method[] methods = o.getClass().getMethods();
for(int i=0;i<methods.length;i++){
Method m = methods[i];
// System.out.println(m.getName());
if(m.getName().equals("setId")){
System.out.println("1111");
m.invoke(o, new Object[]{1L});
}
} //method.invoke(o, new Object[]{1L}); }
添加回答
举报