package com.wen;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.util.ArrayList;
/**
* <p>Title: </p>
* <p>Description:根据传入的class和property,取的属性的值</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class Reflect {
public static String get(Object object, String methodname, int i){
Class cls = object.getClass();
Object ret;
try {
Method method = cls.getMethod(methodname, null);
ret = method.invoke(cls.newInstance(), null);
ArrayList array = new ArrayList();
if (ret.getClass().isArray()) {
ret = Array.get(ret, i);
}
} catch (Exception e) {
ret = null;
}
if(ret == null){
return null;
}
return ret.toString();
}
}
此博客展示了一段Java代码,定义了一个名为Reflect的类,其中包含一个静态方法get。该方法根据传入的对象、方法名和索引,利用反射机制获取对象属性的值,若过程中出现异常则返回null。
188

被折叠的 条评论
为什么被折叠?



